我正在使用PHP创建一个from并进行验证。所有验证完成后,我想隐藏表单并显示成功消息。这是我正在使用的代码,你能告诉我为什么表单总是隐藏并且没有显示成功消息:
<?php
// defining variables and set to empty values
$nameErr = "";
$emailErr = "";
$numberErr = "";
$name = "";
$email = "";
$number = "";
$comment = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
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"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["number"])) {
$numberErr = "Contact Number is required";
} else {
$number = test_input($_POST["number"]);
// check if number only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$number)) {
$numberErr = "Only letters and white space allowed";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
function form_submission_success(){
echo 'Form submitted';
}
function submit_database(){
}
form_submission_success();
} else {
?>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Full Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Contact E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Contact Number: <input type="text" name="number" value="<?php echo $number;?>">
<span class="error">* <?php echo $numberErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php } ?>
使用相同的逻辑,我会将所有条目提交到数据库中,但是一旦所有的条件完成。
答案 0 :(得分:0)
请尝试这个,可能需要更多修改,但可能是一个基本想法
<?php// defining variables and set to empty values
$nameErr = "";
$emailErr = "";
$numberErr = "";
$name = "";
$email = "";
$number = "";
$comment = "";
function test_input($data) {
$data = trim($data);
$data =stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
function form_submission_success($msg = 'Form submitted'){
echo $msg.'<br>';
}
if (empty($_POST["name"])) {
$nameErr = "Name is required";
form_submission_success($nameErr);
} 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";
form_submission_success($nameErr);
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
form_submission_success($emailErr);
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
form_submission_success($emailErr);
}
}
if (empty($_POST["number"])) {
$numberErr = "Contact Number is required";
form_submission_success($numberErr);
} else {
$number = test_input($_POST["number"]);
// check if number only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$number)) {
$numberErr = "Only letters and white space allowed";
form_submission_success($numberErr);
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
function submit_database(){
}
} else {
?>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Full Name: <input type="text" name="name" value="<?php echo $name;?>"
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Contact E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Contact Number: <input type="text" name="number" value="<?php echo $number;?>">
<span class="error">* <?php echo $numberErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php } ?>