Keeping form fields populated after validation with PHP

I am validating successfuly my form when submiting it using PHP. My problem is that if validation fails the form is displayed as new. I want to keep the data from the correctly validated fields. How i will do this?
randomstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tdotCommented:
If the form submit's to itself you can use the post variable to populate the corresponding  input field with the data that was flagged as ok and discard the items flagged as bad...
0
tdotCommented:
example:


$submitted = @$_POST['submitted'];
if($submitted== 'true'){
 
 if(isset($_POST['blah'])){       //DO THIS EACH ITEM
   //input field variable holders here
   $input_blah = $_post['blah']; //example name input field
     
 }else{
  //set some flag here if you want
 
 }
}
in html
 <input type='text'  name='blah' value="'.$input_blah.'"  / >
<input type='hidden'  name='submitted' value='true'>






0
Mark BradyPrincipal Data EngineerCommented:
If you do that some browsers will not post the same data back once posted forward. It's a real pain. Here is how you achieve what you want.

copy the following and save it as 'session.php' in the same folder or somewhere you can find it on your site.

<?php //session.php
//********************// Start a Session and place POSTED values into an array //***********************************//
session_start();
if(is_array($_REQUEST)) {
              foreach($_REQUEST as $key => $value) {
                        $_SESSION[$key]      = $value;
              }
      }
//*******************// End of Session setting up //***************************************************************//
?>




Now, on the page that you have your form in (assuming it is a PHP page) start the page like this:


<?php
include("session.php") // change the path to suit where you saved it

// now let's make a simple form.....

?>
<html>
<head><title></title></head>
<body>
<form method="POST" action="validate.php">
Username : <input type="text" name="user" value="<?php echo $_SESSION['user']; ?>"><br>
Password  : <input type="text" name="password" value="<?php echo $_SESSION['password']; ?>"><br>
<input type="submit" value="Submit"></form>


// ok so this is a VERY basic form with only two input fields. This will work with as many fields as you want. The important things here are the "Include("session.php"); at the start of the page(s) that you want to remember values for. And, the "value=" fileds on the form need to have

 <?php echo $_SESSION['formfield_name']; ?>

in their value field. If there are no values in the form yet, it will just be blank. Once you submit the form you can use your normal method of getting the POST data. Don't forget to add the include statement to the top of your validation script. It must be placed before ANY other echo statement.

Now, if everything checks out on the form, you can add this to kill all the values stored in the session.

session_destroy();


// simple as that, but if for eg the name field was ok but the password field was incorrect or some problem, you can selectively keep certain values so they will be displayed again. Let's say you had 5 input fields on your form as follows:

name
password
confirm_password
address line 1
address line 2

Now the name, password and both address lines seem ok, but the confirm_password does not match or is empty, you would need to send the user back to your form to rectify the problem. Do it like this:

unset($_SESSION['confirm_password']); // now it is no longer held in the session as a variable so your form field will be blank.

I think you get the idea. If you have any problems, let me know.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

HackneyCabCommented:
elvin66, which browsers mess with the value of form data if its posted and placed into the form for user correction?
0
Mark BradyPrincipal Data EngineerCommented:
I was referring to a form that POSTS values to another script. you can rely on then reposting those same values back to the first form as they somehow get lost. In any case, the correct way to do it is to use sessions. Those values are then available in any other page on your website as long as the session is started in each page.
0
HackneyCabCommented:
But sessions are useless if you need to allow the user to modify their feedback in the form, to correct errors.

Which browsers have you seen modifying the data? I need to do some testing.
0
Mark BradyPrincipal Data EngineerCommented:
But sessions are useless if you need to allow the user to modify their feedback in the form, to correct errors.

How so? I use them all the time. When your form is submitted and sent back to the form, ALL the data they type in in in the relevant fields. It is a widely used practice. In any case, I've posted my solution so let the asker decide how he/she want's to proceed. There is more than one way to skin a cat.
0
randomstAuthor Commented:
great solution it worked. very detailed explanation thanks!
0
Mark BradyPrincipal Data EngineerCommented:
Thanks I'm glad it has solved your issue. You can use this method anywhere for future applications as well. Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.