Page 1 of 1

Sending POST back after validation failure

Posted: Thu Aug 28, 2008 11:05 pm
by The_Anomaly
Well, the title says it all. I wrote a validation class that I want to use for my site's registration page. However, when validation fails (i.e. passwords dont' match), the page displays with an error--but the fields are all empty. I'd like to be able to send the POST data back to the page somehow, so the user doesn't have to retype it all.

I've seen various threads regarding sending POST data, and in every one, cURL, comes up. However, by glancing at it on php.net, it didn't seem to be what I needed. It seemed to send data to a page--but not really send the user there, so I could use that data in the page. If the answer is cURL, could you please point me in the right direction with it?

Thanks.

Re: Sending POST back after validation failure

Posted: Thu Aug 28, 2008 11:11 pm
by s.dot
If you're POSTing to the same page that the form is in -- then post is already there and you just need to p/repopulate your form with the post data.

Code: Select all

echo '<input type="text" name="email" value="';
 
if (!empty($_POST['email']))
{
    echo htmlentities($_POST['email'], ENT_QUOTES);
}
 
echo '" />';
If you're POSTing to a different page (such as using the post-redirect-get pattern) you'll need to send the POST array via POST using header() back to the original page to be able to p/repopulate the form.

Re: Sending POST back after validation failure

Posted: Fri Aug 29, 2008 12:13 am
by The_Anomaly
The register form is currently set up with itself as the form action. Conditional statements in the business code of the page check to see if the field is valid, and if not, it sends it back to itself via the Header() function. i.e.:

Code: Select all

 
    //Send 'em back if they fail validation
    if(!$email || !$userName || !$password){
        Header('Location: login.php?error=validation');
        exit;
    }
 
Email, userName and password are booleans assigned false if the field fails validation.

So, since you said the POST should still be there after the Header redirect, but it doesn't seem to be. Perhaps there's a better way to "redirect" to the same page with a query string aside from the header function that would persevere this data? If not, how exactly do I send POST data via the Header? My Google-fu must be weak, as I turn up nothing of any help.

Thank you.

Re: Sending POST back after validation failure

Posted: Fri Aug 29, 2008 12:45 am
by eskio
Hi,

I think it is better to redirect if the login is valid. If you stay on the same page when the validation is not correct you can use this

Code: Select all

$login = addslashes(trim($_POST['txtLogin'])); 
.....
.........
..........
<input name="txtLogin" type="text" id="txtLogin" value="<?=$login ?>" size="20" maxlength="15" />
You will find the input data in you text box

Re: Sending POST back after validation failure

Posted: Fri Aug 29, 2008 3:29 pm
by The_Anomaly
Thanks for the suggestion. I'd do this, but in many of the forms I've implemented so far, Header() is used when validation fails. For this reason, doing what you propose would require a good deal of work. Could someone point me in the right direction regarding sending POST via header? It would save me a good amount of time, and I'm very interested in knowing how to do so.

Re: Sending POST back after validation failure

Posted: Fri Aug 29, 2008 3:45 pm
by DaiWelsh
Sorry to be the bearer of bad news, but I think you are barking up the wrong tree, as I don't think a header redirect can include post data (I have not read the RFC and I am prepared to be proved wrong, but I have not seen it in all my years web coding). You could write a function to encode all the post params into a GET URL for use in the redirect but that would not be very pretty or very reliable if the form data is long. You could also use session but this has other issues (e.g. use of the back button).

I believe you will as suggested be better off changing the logic so failure leaves you on the page and success redirects instead. Better some known pain today than unknown pain tomorrow and for as long as the site remains?

HTH

Dai