Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a simple Html form in my web. When I submit this form and if the data is not valid then it gives the error and all the contents of the form are wiped out. I do not want to wipe out the contents of the html form, so that user do not have to fill all the form again.

How can I do that?
Posted
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 2:30am    
Correct Question -- my 5. Please see my Answer.
--SA

Sure. Here is how I did it.

Instead of having a PHP/HTML page I created PHP code which generates the whole page out of some XML-based template. This template has parameters: the filled-in data in the fields of the forms. When nothing is posted the page is generated without filled-in data — all parameters gets default values, most of them are empty strings. When the POST is done, the same PHP script runs, this time with POST data. This data is used for parameters in the page-generated PHP function. All filled-in data is restored, plus additional data is calculated and added: for example, data validation information, such as errors and recommendation on what should be fixed be the user on next POST.

—SA
 
Share this answer
 
Comments
rashidfarooq 12-Apr-11 8:44am    
Is it the only way to solve this problem?
Sergey Alexandrovich Kryukov 12-Apr-11 15:34pm    
Well, of course not, but this one is where you have some already-developed code such as XML support, so this one is practical enough.

You can follow my logic and think of something else. The code of that is this: when you post, the page is loaded from scratch. The only information you got from the previous page (in a way, the same PHP script URL can behave as different pages depending on data such as POST data, because with PHP you always generate some of the page of all of it), so the only information you got from the previous page is the POST data. What can you do? Isn't this logical?

Regardless of other detail and your design, you essentially do something like that, maybe in different ways...

--SA
Sergey Alexandrovich Kryukov 12-Apr-11 15:38pm    
Basically, the setting is this: you have two source of data: the page with form before you entered and the data filled in by the user (even if you had some data in input edit control and the user deleted few character, this is a new filled-in data). So, to render the page with filled-in data you need both sources of data. You need two containers where this is stored; in your setting POST data is already there, so where can you get the other part? You need some contained where the intact data is stored between client requests.

This is all about it.
--SA
I will demonstrate the solution of your problem with an example. What you need to do is add a value to each of your form inputs. This input field should be PHP parameterized to ensure that on post-back, the data is not lost. For example:

PHP
<?php
if(isset($_POST['form1'])){
   if(!empty($_POST['user']) && !empty($_POST['email'])){
   // Perform your validation steps for your data. If any of the fields above is empty, you will be taken to the else statement that contains your form and you will be required to enter the data appropriately. If the fields are not empty, then you need to perform a validation. So if the data is not valid, you will need to carry out the step below within this if statement. Of course you need to have other if statements within this if statement to get required results.
   }
   else{
      //Then if data is not valid, echo your form back with the data already in
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="hidden" name="form1" value="true" />
   Username: <input type="text" name="user" value="<?php if(isset($_POST['user'])) echo $_POST['user']; ?>" /><br />
   Email: <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /><br />
   <input type="submit" value="Submit" />
</form>
<?php
   }
}
?>


You can get more information on working with PHP post-back from PHP.NET, which is the official PHP Documentation.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900