Sending POST back after validation failure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Sending POST back after validation failure

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Sending POST back after validation failure

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Sending POST back after validation failure

Post 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.
eskio
Forum Commoner
Posts: 66
Joined: Tue Apr 01, 2008 1:00 am

Re: Sending POST back after validation failure

Post 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
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Sending POST back after validation failure

Post 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.
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Re: Sending POST back after validation failure

Post 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
Post Reply