Php 使用<;将信息传递到另一个网页;表格>;在if语句中添加标记
我一直试图只在所有需要填写的字段都已填写时才将信息传递到下一个网页。但是,无论字段是否已填写,信息仍会传递到下一个网页 这是包含字段的网页Php 使用<;将信息传递到另一个网页;表格>;在if语句中添加标记,php,html,Php,Html,我一直试图只在所有需要填写的字段都已填写时才将信息传递到下一个网页。但是,无论字段是否已填写,信息仍会传递到下一个网页 这是包含字段的网页 <!DOCTYPE html> <html lang="en-US"> <head> <title>Book A Table</title> </head> <body> <h1>Book A Table</h1> <?php // de
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Book A Table</title>
</head>
<body>
<h1>Book A Table</h1>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $numErr=$dateErr = $timeErr = $personsErr="";
$name = $email = $num= $date = $time = $persons = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["num"])) {
$numErr = "Number is required";
} else {
$num = test_input($_POST["num"]);
if (!preg_match("([0-9])", $num)) {
$numErr = "Enter numbers only";
}
}
if (empty($_POST["date"])) {
$dateErr = "Date is required";
} else {
$date = test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "Time is required";
} else {
$time = test_input($_POST["time"]);
}
if (empty($_POST["persons"])) {
$personsErr = "Number of persons is required";
} else {
$persons = test_input($_POST["persons"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
//directs all the entered information below to DBInput.php
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Full Name<br> <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail<br> <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Contact Number<br> <input type="text" name="num">
<span class="error">*<?php echo $numErr;?></span>
<br><br>
Reservation Date<br> <input type="date" name="date">
<span class="error">*<?php echo $dateErr;?></span>
<br><br>
Reservation Time<br>(Mon - Thur: 18:00 - 23:00 Fri - Sun: 12:00 - 00:00)<br> <input type="time" name="time">
<span class="error">*<?php echo $timeErr;?></span>
<br><br>
Number of Persons<br> <input type="text" name="persons">
<span class="error">*<?php echo $personsErr;?></span>
<br><br>
Comments<br><textarea name="comment" rows="5" cols="40"></textarea><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($name!=""&&$email!=""&&$num!=""&&$date!=""&&$time!=""&&$persons!=""){ ?>
//directs all the entered information below to DBInput.php
<form action="DBInput.php" method="POST" />
<?php } ?>
</body>
</html>
预定桌子
预定桌子
//将下面输入的所有信息定向到DBInput.php
*必填字段
您正在尝试进行某种奇怪的验证。我见过“自定义”表单验证,但从未见过像这样的
你把事情弄得太复杂了
通常,验证是通过javascript或jQuery进行的,只有当所有变量都已填充时,才可以将ajax请求放入如下条件语句中:
var name = $('#name').val();
var email = $('#email').val();
var num = $('#num').val()
var dqte = $('#date').val()
var time = $('#time').val()
var persons = $('#persons').val()
var comment = $('#comment').val()
if(name !== null && email !== null && ... put here all your conditions ) {
form_data.append('file', file_data);
$.ajax({
url: 'DBInput.php', // point to server-side PHP script
cache: false,
contentType: false,
processData: false,
data: {name: name, email: email, ... },
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});
Cz147有帮助吗?
var name = $('#name').val();
var email = $('#email').val();
var num = $('#num').val()
var dqte = $('#date').val()
var time = $('#time').val()
var persons = $('#persons').val()
var comment = $('#comment').val()
if(name !== null && email !== null && ... put here all your conditions ) {
form_data.append('file', file_data);
$.ajax({
url: 'DBInput.php', // point to server-side PHP script
cache: false,
contentType: false,
processData: false,
data: {name: name, email: email, ... },
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});