Click here to Skip to main content
12,500,835 members (47,231 online)
Rate this:
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
SAKryukov 12-Apr-11 2:30am
Correct Question -- my 5. Please see my Answer.
Rate this: bad
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.

rashidfarooq 12-Apr-11 8:44am
Is it the only way to solve this problem?
SAKryukov 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...

SAKryukov 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.
Rate this: bad
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:

   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.
      //Then if data is not valid, echo your form back with the data already in

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2016
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