刷新时,详细信息不会保留在表单中

时间:2014-05-20 11:45:26

标签: php forms

我已经与服务器端验证建立了联系表单,它工作正常。输入带有未被注意的错误的信息时,表单将返回以突出显示它。当我返回之前填写的字段为空时,无论该字段是否有错误。我希望将表单返回给用户,并将其放入文件的信息保留在那里,直到表单通过验证。

这是我验证PHP的代码

<?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>

由于

2 个答案:

答案 0 :(得分:2)

如果表单操作是同一页面,即用于验证的php代码,并且所有页面都在同一页面上,则可以将post / get值回显到除密码之外的相应字段。否则,在会话变量或cookie上存储post / get值,然后将这些值作为除密码之外的相应字段的字段值进行回显。

答案 1 :(得分:-1)

这是你的解决方案

<强> 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>

<强> form_submit.php

<?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");
  }
?>