Php 刷新时,表单中不会保留详细信息
我已经制作了一个带有服务器端验证的联系表单,它工作得很好。当输入信息时出现未被注意到的错误,表单将返回并突出显示。当我返回之前填写的字段时,无论该字段是否有错误,这些字段都是空的。我希望将表单返回给用户,并将他们放入文件中的信息保留在那里,直到表单通过验证 这是我验证PHP的代码Php 刷新时,表单中不会保留详细信息,php,forms,Php,Forms,我已经制作了一个带有服务器端验证的联系表单,它工作得很好。当输入信息时出现未被注意到的错误,表单将返回并突出显示。当我返回之前填写的字段时,无论该字段是否有错误,这些字段都是空的。我希望将表单返回给用户,并将他们放入文件中的信息保留在那里,直到表单通过验证 这是我验证PHP的代码 <?php // define variables and set to empty values $firstnameErr = $secondnameErr = $emailaddressErr = $com
<?php
// define variables and set to empty values
$firstnameErr = $secondnameErr = $emailaddressErr = $commentErr = $captchaErr = "";
$firstname = $email = $secondname = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$firstnameErr = "First name is required";
} else {
$firstname = test_input($_POST["firstname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$firstnameErr = "Invalid first name";
}
}
if (empty($_POST["secondname"])) {
$secondnameErr = "Second name is required";
} else {
$secondname = test_input($_POST["secondname"]);
// check if e-mail address syntax is valid
if (!preg_match("/^[a-zA-Z ]*$/",$secondname)) {
$secondnameErr = "Invalid second name";
}
}
if (empty($_POST["emailaddress"])) {
$emailaddressErr = "Email address is required";
} else {
$emailaddress = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$emailaddress)) {
$emailaddressErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Enter a message";
} else {
$comment = test_input($_POST["comment"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$comment)) {
$commentErr = "Only letters and white space allowed";
}
}
if (empty($_POST["captcha"])) {
$captchaErr = "Enter the answer to the sum";
} else {
$captcha = test_input($_POST["captcha"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$captcha)) {
$captchaErr = "Only letters and white space allowed";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
这是我的表格
<form name="Contact" form id="Contact" onsubmit=" return validate()" METHOD="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name" required />
<span class="error"><?php echo $firstnameErr;?></span> </div>
<!--End input-->
</div> <!--End row--><br />
<div class="Row">
<div class="Lable">Second Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="secondname" class="detail" name="secondname" placeholder="Second Name" required />
<span class="error"><?php echo $secondnameErr;?></span> </div>
<!--End input-->
</div> <!--End row-->
<br />
<div class="Row">
<div class="Lable">Email Address:</div> <!--End of Lable-->
<div class="input">
<input type="email" id="emailaddress" class="detail" name="emailaddress" placeholder="Email Address" />
<span class="error"><?php echo $emailaddressErr;?></span>
</div> <!--End input-->
</div> <!--End row-->
<br />
<div class="Row">
<div class="Lable">Your Message:</div> <!--End of Lable-->
<div class="input">
<textarea id="comment" name="comment" class="mess" placeholder="Your Message" minlength="10" required ></textarea>
<span class="error"><?php echo $commentErr;?></span>
</div> <!--End input-->
</div> <!--End row-->
<br />
<input id="number1" name="number1" readonly="readonly" class="Add" value="<?php echo rand(1,4) ?>" /> +
<input id="number2" name="number2" readonly="readonly" class="Add" value="<?php echo rand(5,9) ?>" /> =
<input type="text" name="captcha" id="captcha" class="captcha" maxlength="2" />
<div class="Lable">Please give the correct answer to the sum</div>
<br />
<span class="captchaerror"><?php echo $captchaErr;?></span>
<br />
<br />
<div class="submit">
<input type="submit" id="send" Name="send" value="Send" />
</div><!--End of submit-->
<div class="Clear">
<input type="reset" id="clear" Name="Clear" value="Clear" />
</div>
这是您的解决方案
form_page.php
<form action="form_submit.php" method="post">
<input type="text" id="text1" name="text1" value="<?php echo @$_POST['text1'];?>" />
<input type="text" id="text2" name="text2" value="<?php echo @$_POST['text2'];?>" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
$error = false;
if($text1=="")
{
$error = true;
echo "Text 1 is blank";
}
else if($text1=="")
{
$error = true;
echo "Text 2 is blank";
}
else
{
// do your work
}
if($error)
{
include("form_page.php");
}
else
{
header(location:"form_page.php");
}
?>
如果表单操作是同一页面,即用于验证的php代码,并且所有代码都在同一页面上,则可以将post/get值回显到除密码之外的相应字段。否则,将post/get值存储在会话变量上或存储为cookie,然后将这些值作为相应字段(密码除外)的字段值进行回显 确保在php脚本中有session_start,在这里有表单并进行验证。此外,您还需要在会话中保存每个字段的值。如果不显示代码,则无法向您提供建议。我们不知道您的网站是如何工作的。请确保您正在以某种方式(如会话)保存这些值,然后在刷新时将它们放回表单中。@itachi您是否正确地看到了代码。第一次加载页面时,未设置$\u POST['text1'@
@跳过该错误。当页面提交并出现任何错误时,($error=true)
在form\u submit.php中包含form\u page.php
现在$\u POST['text1']可用,并显示在提交前键入的文本框中。-1代表@
不是任何解决方案。以上表示一个非常糟糕的代码习惯。除了@
之外,唯一的解决方案是value=“”代码>