Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTML PHP
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 11-Apr-11 20:17pm
Comments
SAKryukov at 12-Apr-11 2:30am
   
Correct Question -- my 5. Please see my Answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
rashidfarooq at 12-Apr-11 8:44am
   
Is it the only way to solve this problem?
SAKryukov at 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
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
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.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 518
1 OriginalGriff 459
2 ChintanShukla 305
3 Richard Deeming 275
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,858
1 OriginalGriff 7,451
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100