我基本上是想
我的表单的HTML。它只是一个带有粘性PHP的反馈表。
<form method="post" action="">
<fieldset>
<legend>Contact Form</legend>
<label>Name (Required)</label>
<input name="name" type="text" placeholder="Enter your name. (e.g. Arthur)" value="<?php if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']);
?>">
<span class="err"><?php echo $nameErr;?></span>
<label>Email (Required)</label>
<input name="email" type="email" placeholder="Enter a valid e-mail. (e.g. someone@host.com)" value="<?php if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']);
?>">
<span class="err"><?php echo $emailErr;?></span>
<?php
if (isset($_POST['reason']))
{
$reasonVar = $_POST['reason'];
}
?>
<label>Reason</label>
<select name="reason">
<option <?php if($reasonVar=="question") echo 'selected="selected"'; ?> value="question">Question</option>
<option <?php if($reasonVar=="feedback") echo 'selected="selected"'; ?> value="feedback">Feedback</option>
<option <?php if($reasonVar=="suggestions") echo 'selected="selected"'; ?> value="suggestions">Suggestions</option>
<option <?php if($reasonVar=="other") echo 'selected="selected"'; ?> value="other">Other</option>
</select>
<label>Message (Required)</label>
<textarea name="message" placeholder="Enter your message. (e.g. Your website is awesome!)" ><?php if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']);?></textarea>
<span class="err"><?php echo $messageErr;?></span>
<input class="sub" name="submit" type="submit" value="Submit">
</fieldset>
</form>
这是该页面的PHP。它只是在表单上进行基本验证,并尝试在所有验证成功后使用header函数将页面重定向到response.php(因此使用long if语句)。
<?php
$nameErr = $name = "";
$emailErr = $email ="";
$messageErr = $message ="";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["name"]))){
$nameErr="Missing";
}
else{
$name = $_POST["name"];
}
if(empty(trim($_POST["email"]))){
$emailErr = "Missing";
}
elseif(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
$emailErr = "Invalid";
}
else{
$email=$_POST["email"];
}
if(empty(trim($_POST["message"]))){
$messageErr="Missing";
}
else{
$message = $_POST["message"];
}
if(!empty(trim($_POST["name"]))&&!empty(trim($_POST["email"]))&&filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)&&!empty(trim($_POST["message"]))){
header("Location:response.php");
exit;
}
}
?>
现在,如果表单有效,表单就会消失,我会停留在表单页面上,而不会重定向到成功页面。我对网络编码的后端非常新,所以任何帮助都会非常感激。非常感谢你!