1

I have user registration form which collects all necesary information. When user checks the box "I Agree all terms and conditions", submit button is enabled so user can submit form. I am posting this data to another php page which does validation and if any error is found, registration form appears again showing certain errors that some necessary information is missing.

Problem: I am unable to retain the values of the form when user is redirected back to registration form though i have putted the following code:

<input type="text" size="40" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : NULL; ?>" />

I am using

header("location: form.php");

when errors are found in my php page which does validation. Is this line creating a problem for me or is there anything else? I am unable to figure out the problem.

EDIT: In addition if any one can guide me that how can i retain the values selected through radio button and combo box i'll be thankfull.

kamwo
  • 1,980
  • 1
  • 23
  • 32
Nouman
  • 45
  • 1
  • 1
  • 8
  • 1
    Post values will be deleted after redirecting. – Lkopo Aug 31 '13 at 10:00
  • if any error is found, redirect it to form page with any array having the posted values and fill the form values – Deepanshu Goyal Aug 31 '13 at 10:02
  • http://php.net/manual/en/function.header.php, your doing it wrong. you need to call the
    .php file. Post more of your example and it will be easier to help you.
    – Roger Aug 31 '13 at 10:03

3 Answers3

1

As many others have pointed out in the comments, you're doing it wrong.

BUT, fear not, there is a simple method to achieve what you want.

Firstly, understand that you cannot access $_POST values after redirection. If you want to know why?

Because: HTTP is a stateless protocol. (Read this answer before continuing).

Now I understand that HTTP is stateless. So, how do I maintain form states?

Well, generally people keep the validation logic on the same page. So, considering that your registration form is located at form.php , the form action will point to form.php itself instead of any success page.

Whenever form.php receives a request, it checks if the request is from a form submit or fresh call. If it is a form submit the validation logic is executed. In case of any errors being found, the page is re-emmited and values are populated by the same way as you are trying to do it now. (BUT, this time the post variables are available, because there was no redirection.)

If there are no errors then you redirect visitor to confirmation page etc.

If the request is a first request (i.e. not from a form submit). You can skip the validation part and directly emit the form html with defaults.

If you are still wondering how to detect if the request is from a form submit or not, there is a simple method to do it. In your form, add a hidden field named hdnVar with value set to yes or anything that you like. Then on every request to form.php simply check if $_POST['hdnvar'] is present, if it is present and the value is as you've set it, then the request is from a form submit.

Hope I could make myself clear. Ask in case of any doubts :)

Cheers!

Community
  • 1
  • 1
Sunny R Gupta
  • 5,026
  • 1
  • 31
  • 40
  • so after successfull validation what link should i redirect to and how? – Nouman Aug 31 '13 at 10:21
  • I just recommended one way of doing validation, next what you do with the data is entirely upto you. Are you trying to add the validated data to SQL? Then do it on the same page and then redirect the user to a login page. – Sunny R Gupta Aug 31 '13 at 10:25
  • yes the validated data will be stored to database through SQL, i have no problem in saving the information. Upon successfull validation and saving the data to databse i want to display the message that **your information has been added successfully** on the same page where form lies and this inforamtion will be shown in a div which will be visible only on successfull validation. – Nouman Aug 31 '13 at 10:29
  • Well then no need to redirect the user at all. (Even Better). When ever you have validated the user info and added him/her to the database, simply `echo` a div in your HTML above the form with the content you want to show. A simple way would be to create a variable `$isAdded` and assign it the value `1`. While printing the HTML, if `$isAdded` is 1, then you can `echo` the div too. – Sunny R Gupta Aug 31 '13 at 10:37
  • i have putted my validation code on **form.php** and put a check whether submit button is pressed or not, if yes validations are done and page **form.php** is displayed, if not display the page as it is, but still i am unable to retain the form field's values... :( – Nouman Aug 31 '13 at 17:43
  • Now you have to include the input boxes like you were trying earlier: ` ` are you doing this? – Sunny R Gupta Aug 31 '13 at 17:53
  • yes i am including this, i am using `$_SESSION` is this creating problem?? – Nouman Aug 31 '13 at 17:59
  • What exactly are you using session for? But session will not interfere with $_Post variables. – Sunny R Gupta Aug 31 '13 at 18:45
-1

If I'm using any kind of validation I, as a rule of thumb, use action="<?php $_SERVER['REQUEST_URI']" and handle any necessary action on the same page as the form IE

if(isset($_POST['an_expected_input']) {
    // First validate
    if($is_valid) {
        // Do action with the post data
        // Posibly redirect to a confirmation page
    } else {
        // Do whatever needed for feedback to the visitor
        // Add posted values back to the form
    }
}

This is one way that I found works in 99% of my cases. Maybe it won't work for you but at least you have one more approach to consider now :)

Anders
  • 499
  • 1
  • 5
  • 18
  • Hi, your code will never throw an error, BUT, it will never achieve what the OP is trying to do. Since, the OP is using a redirect to another URL, due to the stateless nature of HTTP all data would be lost unless he stops redirecting back to the form page. – Sunny R Gupta Aug 31 '13 at 10:22
  • Yeah that is true. I guess I gave a solution that was more of a new approach instead of really explaining why this will work and why his isn't. Sunny explained it better. – Anders Aug 31 '13 at 10:24
  • Your method looks good to me, sans explanation. Basically we are so used to doing this stuff that we never realize the various techniques people apply to do simple stuff like validation. – Sunny R Gupta Aug 31 '13 at 10:27
-2

try this.

<input type="text" size="40" name="email" value="<?php echo (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : "" ?>" />
user2727841
  • 715
  • 6
  • 21