I have multiple enquiry forms and have different fields. I want to check validation for each field I have in different forms with ajax
Below is my example code:
if($name && $email && $mobile && $departuredate && $vehicle && $query){
?>
<form action="<?php echo DOMAIN; ?>contact/booking-form.php" method="post">
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<input type="hidden" name="mobile" value="<?php echo $mobile; ?>" />
<?php if(isset($_POST['vehicle'])) {?>
<input type="hidden" name="vehicle" value="<?php echo $vehicle; ?>" />
<?php } ?>
<input type="hidden" name="departuredate" value="<?php echo $departuredate; ?>" />
<input type="hidden" name="query" value="<?php echo $query; ?>" />
<input type="hidden" name="enquiry" value="<?php echo $enquiry; ?>" />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="hidden" name="agree" value="<?php echo $agree; ?>" />
<input type="hidden" name="submit" id="submit">
</form>
<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>
<?php } ?>
In given code $vehicle is not fix (sometime this value is blank). How can I check if form has vehicle field then check empty and proceed
You can use this same if statement in your javascript
<?php if(isset($_POST['vehicle'])) {?>
//if the field exists write your js validation otherwise . . .
<?php } ?>
List all the fields
Gather all the required fields
Validate
If all validated, process the data
booking-form.php
<?php
// Fields
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$departuredate = $_POST['departuredate'];
$vehicle = $_POST['vehicle'];
$query = $_POST['query'];
// other fields
// A set of required fields
$required_fields = array(
$name, $email, $mobile, $departuredate, $vehicle, $query
);
$message = [];
$is_ok = 1;
// Validate
foreach ($required_fields as $each) {
if (empty($each)) {
$message[] = 'Fields with (*) are required';
$is_ok = 0;
}
}
// If all validated
if ($is_ok) {
// process the data
}
?>
<ul class="errors">
<?php
// Display errors in list
foreach ($message as $each_msg) {
echo "<li>$each_msg</li>";
}
?>
</ul>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<input type="hidden" name="mobile" value="<?php echo $mobile; ?>" />
<?php if (isset($_POST['vehicle'])) { ?>
<input type="hidden" name="vehicle" value="<?php echo $vehicle; ?>" />
<?php } ?>
<input type="hidden" name="departuredate" value="<?php echo $departuredate; ?>" />
<input type="hidden" name="query" value="<?php echo $query; ?>" />
<input type="hidden" name="enquiry" value="<?php echo $enquiry; ?>" />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="hidden" name="agree" value="<?php echo $agree; ?>" />
<input type="hidden" name="submit" id="submit">
</form>
<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>
Hi you can use like this.
<?php
function function1() {
echo "this is function 1";
}
function function2() {
echo "this is function 2";
}
if ($page)
eval("{$page} ();");
echo "<a href='$PHP_SELF?page=function1'>Function 1</a>";
echo "<a href='$PHP_SELF?page=function2'>Function 2</a>";
?>
Related
I don't know how to get the value, with js always show me the first input value! Thanks in advance!!!
<html>
<head>
</head>
<body>
<?php
while($i < $forid){
?>
<div id="mydiv">
<input type="text" id="myid" name="myid" value="<?php echo $arrayid[$i]; ?>">
<input type="text" id="myname" name="myname" value="<?php echo $arrayname[$i];?>">
<input type="text" id="myjob"name="myjob" value="<?php echo $arrayjob[$i]; ?>">
<input type="submit" value="get values" id="getvaluesbutton">
</div>
<?php
$i++;
}
?>
</body>
</html>
Get array access to elements by class/name in your js script by changing html:
<?php
while($i < $forid){
?>
<div id="mydiv">
<input type="text" name="myid[]" class="myid" value="<?php echo $arrayid[$i]; ?>">
<input type="text" name="myname[]" class="myname" value="<?php echo $arrayname[$i];?>">
<input type="text" name="myjob[]" class="myjob" value="<?php echo $arrayjob[$i]; ?>">
<input type="submit" value="get values" id="getvaluesbutton">
</div>
<?php
$i++;
}
?>
for javascript hints read this question
I have a set of inputs that are dynamically generated and their id's incremented based on the number of items in an array
<table width="100%">
<?php $i = 1; ?>
<?php foreach ($products as $product) { ?>
<tr>
<td>
<p><b><?php echo $product['name']; ?></b></p>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<div class="form-group">
<label class="control-label" for="firstname_<?php echo $i; ?>">First Name:</label>
<input name="firstname_<?php echo $i; ?>" id="firstname_<?php echo $i; ?>" required>
</div>
</td>
<td>
<div class="form-group">
<label class="control-label" for="lastname_<?php echo $i; ?>">Last Name:</label>
<input name="lastname_<?php echo $i; ?>" id="lastname_<?php echo $i; ?>" required>
</div>
</td>
<td>
<div class="form-group">
<label class="control-label" for="recipient_number_<?php echo $i; ?>"> Phone Number:</label>
<input name="recipient_number_<?php echo $i; ?>" id="recipient_number_<?php echo $i; ?>" required>
</div>
</td>
<td>
<div class="form-group">
<label class="control-label" for="recipient_email_<?php echo $i; ?>"> Email:</label>
<input name="recipient_number_<?php echo $i; ?>" id="recipient_email_<?php echo $i; ?>" required>
</div>
</td>
</tr>
<?php $i++; ?>
<?php } ?>
</table>
I am trying to look for a way using JQuery to loop through all the generated inputs to ensure they are not empty
Try this snippet.
This will allow you to validate the fields which are empty, and try to use the filter, that helps you to do it without any loop.
More information filter
$('#sender_container').on('submit',function(e)
{
e.preventDefault();
validate();
});
function validate()
{
$('#sender_container > input[type="text"]')
.removeClass('error')
.filter(function() {
// Remove error classes. Filter
return !$.trim(this.value);
})
.addClass('error');
}
.error {
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="sender_container">
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="submit" name="submit" />
</form>
try using this -
you can replace table with a table Id here - $('#tableId :input')
$('table :input').each(function(){
//Enter your code to validate input
console.log($(this).attr('name') + " : " + $(this).val());
});
I have a conditional statement set on the url,I tested both the IF and ELSE and it does work with a simple document.write. However, I am trying to output Php i the conditional but it just always outputs the else statement, Php is not my strongest language:
<script type="text/javascript">
if(
window.location.href=="mywebsite.com"
)
{
document.write('<label for="qty"><?php echo $this->__("Qty:") ?></label><input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />')
}else{
document.write('<label for="qty"><?php echo $this->__(' ') ?></label>
<input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />')
}
</script>
window.location.href contains the full URL. If you just want to check the hostname part of the URL, use window.location.host.
if (window.location.host == "mywebsite.com") {
...
}
Another option would be to do all the work on the server.
<?php
if ($_SERVER['SERVER_NAME'] == 'mywebsite.com') {
?>
<label for="qty"><?php echo $this->__("Qty:") ?></label>
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />
<?php } else {
?>
<label for="qty"><?php echo $this->__(' ') ?></label>
<input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />
<?php }
window.location.href returns the full URL including protocol (so http://www.mywebsite.com).
You could alert the value of window.location.href to see exactly what it's returning, and update your if statement to match it.
I have a situation where I want to auto submit form in cakephp ctp file. Can anyone please help me? I know there are number of same or similar question have asked before this. It tried everything, but it doesn't work. Please have a look on my code:
SCRIPT PART:
<script>
window.onload = function() {
alert("reached here");
document.frmTransaction.submit();
}
</script>
HTML PART:
<form method="post" action="https://secure.ebs.in/pg/ma/sale/pay" name="frmTransaction" id="frmTransaction" onSubmit="return validate()" onLoad="javascript:autoSubmit();">
<input name="account_id" type="hidden" value="<?echo $_POST['account_id'] ?>" />
<input name="return_url" type="hidden" size="60" value="<?echo $_POST['return_url'] ?>" />
<input name="mode" type="hidden" size="60" value="<? echo $_POST['mode']?>" />
<table style="width:50%">
<tr>
<td>Reference: </td><td><input name="reference_no" type="text" value="<? echo $_POST['data']['reference_no'] ?>" readonly/></td>
</tr>
<tr>
<td>Amount: </td><td><input name="amount" type="text" value="<? echo $_POST['data']['amount'] ?>" readonly/>Rs.</td>
</tr>
</table>
<input name="description" type="hidden" value="<? echo $_POST['description'] ?>" />
<input name="name" type="hidden" value="<? echo $_POST['name'] ?>" />
<input name="email" type="hidden" value="<? echo $_POST['email'] ?>" />
<input name="phone" type="hidden" value="<? echo $_POST['phone'] ?>" />
<input name="address" type="hidden" value="<? echo $_POST['address'] ?>" />
<input name="country" type="hidden" value="IND" />
<input name="postal_code" type="hidden" value="<? echo $_POST['postal'] ?>" />
<input name="state" type="hidden" value="<? echo $_POST['state'] ?>" />
<input name="city" type="hidden" value="<? echo $_POST['city'] ?>" />
<input name="secure_hash" type="hidden" size="60" value="<? echo $secure_hash;?>" />
<br/><br/><br/><input name="submit" value="Submit" type="submit" id="submit" style="background:#ff6600;color:#fff;height:40px;width:150px;font-family:Calibri;font-size: 22px;"/>
</form>
You have to check form definition again.
Remove onload event statement.
<form method="post" action="https://secure.ebs.in/pg/ma/sale/pay" name="frmTransaction" id="frmTransaction" onSubmit="return validate()">
I have a simple javascript issue. I have a checkbox and a input box.
when the user inputs text into an input box (onkeydown) I want it to check the corresponding checkbox in the row.
My checkbox's all share the same name for an array checkbox[].
my simplified syntax is:
<table>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
</td>
<tr>
<?php endforeach ; ?>
</table>
As the name of the checkbox is not unique, only the value thereof how do I tell the javascript which checkbox to check?
<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
Help appreciated as always.
Thanks
HTML
<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name1" id="name1" />
<br>
<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name2" id="name2" />
<br>
<input type="checkbox" name="checkbox[]" />
<input type="text" name="name3" id="name3" />
Jquery
$(':text').change(function(){
$(this).prev(':checkbox').attr('checked','true');
});
Hope this helps.
Regards.