Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: keeping filled form field values after form errors

Tags:

forms

php

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?

like image 803
NerdsRaisedHand Avatar asked Dec 11 '22 11:12

NerdsRaisedHand


1 Answers

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.

like image 115
Govind Rai Avatar answered Dec 29 '22 23:12

Govind Rai