我只是在填写了所有需要填写的字段时才尝试将信息传递到下一个网页。但无论字段是否填满,信息仍会传递到下一个网页。 / p>
这是包含字段
的网页<!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>
这是从字段接收信息并将其存储在数据库中的网站
<?php
$servername = "localhost";
$username = "Client";
$dbname= "webassigment";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//stores the values entered in book.php in each value
$value1=$_POST['name'];
$value2=$_POST['email'];
$value3=$_POST['num'];
$value4=$_POST['date'];
$value5=$_POST['time'];
$value6=$_POST['persons'];
$value7=$_POST['comment'];
//puts the values in each respective database field
$sql = "INSERT INTO booking (Name,Email, ContactNumber,ReservationDate ,Reservationtime, NumberOfPeople,Comment)
VALUES ('$value1', '$value2', '$value3','$value4','$value5','$value6','$value7')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
echo "Connected successfully";
?>
答案 0 :(得分:0)
您正在尝试进行一些奇怪的验证。我已经看过&#34; custom&#34;形式验证,但从未像这样。
你的事情过于复杂。
通常验证是通过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
}
});
});