tags:

views:

1653

answers:

5

Well right now I have a page (page2.php) and at the top it does some validation checking and if something fails it bounces back to page1.php. The problem is that page1.php is loaded by a post meaning it is the end result of a form. This means that all the post data I originally have on page1.php is lost.

So here is my code:

if ($validation_fails)
{
    header('Location:page1.php');
}
+4  A: 

You can post data back with cURL or re-structure to do the validation at the top of page1.php and if it doesn't fail, take to page2.php. If you are doing some sort of multi-step form, you could save all the data in a session and populate fields if there's matching data in the session. Not sure if that applies, though.

Paolo Bergantino
+2  A: 

Can’t you just include the validation script via include or require instead of redirecting to it?

Gumbo
+2  A: 

You could restructure the logic. The validation should happen on page1.php and if it fails, the redirection does not happen. If it succeeds, you are redirected.

If you are concerned about safety (people just going to page2), you could set a session variable that is checked on page2 and set on page1.

Ólafur Waage
+1  A: 

On page2.php You could store the contents of the post into a $_SESSION variable if validation fails.

On page1.php you simply include a properly escaped/encoded version of the data that you stored in the session as part of the form. You would also be able to use this to update the form so it is clear the user what part failed the validation.

Zoredache
A: 

I also want to post data (XML, but that shouldn't matter) with a redirect.

I currently found that with PEAR you can make a request and get the response:

require_once('HTTP/Request.php'); $req =& new HTTP_Request($url); $req->addHeader("Content-Type", "text/xml"); $req->addHeader("Content-Length", strlen($xml)); $req->setMethod(HTTP_REQUEST_METHOD_POST); $req->addRawPostData($xml, true); $req->sendRequest(); echo $req->getResponseBody();

This will make the response body show in you browser. So i'm almost there. But i want the user to really go to that url, instead the page calling it and just display the body. Because like this, i only get the HTML, but the CSS and other included files are missing. Also it has some relative urls, so if i click one, it fails, because i'm still at the original domain. (the url points to another domain).

so i want i sort of header('Location:'.$url), but then with POST data included.

EDIT

I solved this as follows:

$xml = “<xml>...” $postHtml = "<html>"; $postHtml .= "<body>"; $postHtml .= "<form id='myform' name='myform' method='POST' action='".$domain."/Purchase.aspx?a=123'>"; $postHtml .= "<input type='hidden' id='OrderXml' name='OrderXml' value='".$xml."' />"; $postHtml .= "</form>"; $postHtml .= "<script lang='Javascript'>document.myform.submit();</script>"; $postHtml .= "</body>"; $postHtml .= "</html>"; echo $postHtml;