我有多个查询表格,并且有不同的字段。我想用ajax
检查我用不同形式的每个字段的验证以下是我的示例代码:
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 } ?>
在给定代码中,$ vehicle未修复(有时此值为空)。如何检查表格是否有车辆字段然后检查为空并继续
答案 0 :(得分:0)
您可以在javascript中使用相同的if
语句
<?php if(isset($_POST['vehicle'])) {?>
//if the field exists write your js validation otherwise . . .
<?php } ?>
答案 1 :(得分:0)
预订-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>
答案 2 :(得分:-1)
嗨,你可以这样使用。
<?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>";
?>