Forum Moderators: coopster

Message Too Old, No Replies

post headers?

keeping the forms post data?

         

ahmedtheking

11:36 pm on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ive created a form that is validated by another php script. If there is an error with the form (eg a field was left out) the script sends the user back to the form with an error statement. But all the data that was entered into the form has been lost! Is there any way to send the $_POST data back?

grandpa

4:05 am on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Put a form on the second page, and send it back. Use hidden fields on the second form.

For something like this I might prefer to do everything in a single script.

if ($validating_form_data) {
// Do validation here
// store data in hidden fields, exactly as they
// were done originally
if ($did_not_validate) {
// display resubmit button
}
}
else {
// Collect form data here and submit
}

There's obviously a lot of processing missing from this, but maybe it gives you the idea.

dreamcatcher

7:32 am on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



grandpa`s method would work, but you don`t really need a second form.

Set up your form to process, but have it refresh the same page and load the values in the input boxes if something has been left out.

Set up a variable when you process. If everything is ok, set it to true and display thanks page. If not just reshow the form.

Example:

<?php

$PROCESS = false;

if (isset($_POST['send']))
{
$name = trim($_POST['name']);
$email = trim($_POST['email']);

if (!$name=="" &&!$email=="")
{
//do mail stuff

$PROCESS = true;
}
}

if (!$PROCESS)
{

?>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="send" value="1">
<table>
<tr>
<td>NAME:</td>
<td><input type="text" name="name" value="<?php echo (isset($_POST['name'])? $name : "");?>"></td>
</tr>
<tr>
<td>E-MAIL:</td>
<td><input type="text" name="email" value="<?php echo (isset($_POST['email'])? $email: "");?>"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Send"></td>
</tr>
</table>
</form>
<?php

}
else
{

?>
<p>Thank you! Message sent!</p>
<?php

}

?>

Thats really cheap and nasty, but you should get the idea.

dc

ahmedtheking

7:50 am on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought of having both those ideas, but because my form sits about 3 requires down the site, it would just be a nightmare to get it working, that's why i validate in a different script.

Isn't there a way to array the $_POST vars, stick them in the redirect header or a way to make the user go back, like javascript where it's the same as the back button on the browser (because then the vars are still there!)

grandpa

10:29 am on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you store the data in a cookie? Then if you get sent back you can read the cookie and fill in the form.

What is the action that returns you to the originating page if the data is invalid?
Is it automatic?
Do you display a error message and show a link?

In the validating script I would display the error and provide a button. Now you have everything in a form ready to POST back. Then modify the first script to accept that POST, and to go to the correct part of the script.

If you can do it with headers then it's probably easier, but I'm not the person that can help you with headers... I only do convoluted processes :) Even if you did send a page header, won't you will still have to work out how to get to the correct part of the original script? I believe you mentioned it was a few steps in before you POST for validation.

ahmedtheking

11:43 am on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, I kind of understand!

The form sends POST values to the validation script. Then if validation fails, it's sent back to the page ( header -> $HTTP_REFERER )

If I use a cookie, how would it work? I had the idea of passing the info back in the header URL but it exceeds 255 chars!