PHP: keeping filled form field values after form errors

*爱你&永不变心* 提交于 2021-02-07 17:26:14

问题


I ve read a few question on the site similar to this but couldn't really take anything from them. I have a form that gets submitted, if there are errors, I notify the user of the errors above the form and display the form below that for them to correct it. This is all good and dandy except that they have to re enter all of their information again. I want the information to stay, and them to only have to fix / reenter the field that the mistake was in.

Here is my handling file:

  <?php

 include_once("Header.php");

 if(!empty($_POST['formsubmit'])){require_once ("Form_Handle.php");}
 include("Form.php");

 include_once("Footer.php");


 ?>

is there something that i have to do when I include the form.php after I handle it?


回答1:


I felt the answers provided are somewhat vague and also incorrect in the sense they tell you to set the contents of the fields inside the value attribute based on whether or not values exist for those fields.

Here's why. When you put if statement logic inside the value attribute, it either sets the value attribute to 'YOURVALUE' or '""'. <- That is the problem. The value of the field gets set to empty string when $_POST["field_name"] is empty. If you had form validation and were hoping to throw an 'empty field' error, this would pass your validation logic, which would be completely incorrect (Your form field will appear empty, but there would be an empty string inside it).

Instead, just echo the variable without any checks. If it's not set, nothing will happen. If it is, you will be able to retain the value. For example,

<input type="text" name="email" value="<?php echo $_POST["email"]; ?>" >

Here's another example of code where I forgo the whole above situation and only echo the value attribute if the variables are not empty.

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

   Name: <input type="text" name="name" <?php if (!empty($_POST['name'])) {echo "value=\"" . $_POST["name"] . "\"";} ?> >
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>

   E-mail: <input type="text" name="email" <?php if (!empty($_POST['email'])) {echo "value=\"" . $_POST["email"] . "\"";} ?> >
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>

   Website: <input type="text" name="website" <?php if (!empty($_POST['website'])) {echo "value=\"" . $_POST["website"] . "\"";} ?> >
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>

   Comment: <textarea name="comment" <?php if (!empty($_POST['comment'])) {echo "value=\"" . $_POST["comment"] . "\"";} ?> rows="5" cols="40"></textarea>
   <br><br>

   Gender:
   <input type="radio" name="gender" value="female" <?php if ($_POST['gender']=="female") {echo "checked";} ?> >Female
   <input type="radio" name="gender" value="male" <?php if ($_POST['gender']=="male") {echo "checked";} ?> >Male
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>

   <input type="submit" name="submit" value="Submit"> 

</form>

In relation to the actual question that the user asked, you would need to add similar code into your form.php file.




回答2:


You just need to write some logic in your input field value for keeping entered value. e.g

<input type="text" name="login" value="<?php if(isset($_POST['login'])){ echo $_POST['login'];}?>">



回答3:


In your Form.php you should check the presence of various $_POST vars which you use and put their contents as value of input fields




回答4:


In your form assign default values.

For example:

<?php
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';
?>

And the form field:

<input type="text" name="answer" value="<?php print $answer;?>"/>



回答5:


Why not store the posted data in a session variable.

Then when you redirect the user back to the form page, you can check to see if the form values are in the session variable, if so fill in the contents as the value in the input field.



来源:https://stackoverflow.com/questions/19033275/php-keeping-filled-form-field-values-after-form-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!