tags:

views:

4947

answers:

6

Searching did not find a similar question, so: How can POST data used by PHP to generate one page be passed from that page to another PHP generated page? I have:

  • A form on page 1 which sends data to a PHP script via POST.
  • Page 2 is generated by that script and shows one or more graphs generated by an external program using the entries on page 1 and the back end database. This page also has another form with options to re-generate the graphs with new options (e.g. zoom in on or truncate the graph(s)).
  • If requested, page 3 will be generated with the same PHP script using POST data glued together from pages 1 and 2. Except for the graphs, its basic appearance will be the same as page 2.
  • Pages 4, 5, 6 ... should be generated in the same manner as page 3.

So, how can I put the POST data used to generate page 2 into the POST data for page 3?

EDIT: Due to organizational policy, cookies can't be used (so sessions are not feasible). GET is undesirable because we don't want the input to show in the URL.

+8  A: 

I recall struggling with this issue long ago, wondering why I simply couldn't redirect with a modified POST header. The reason is a redirect is actually considered a GET.

Regardless, you need to store the post variables in hidden fields.

<input type="hidden" name="someValueFromPageOne" value="blah">

I would recommend prefixing all your field names from each form so that its easy to tell them apart during your consolidation phase at the end.

<input type="hidden" name="pageOne_firstName" value="Joe">
<input type="hidden" name="pageTwo_streetNumber" value="22">
Soviut
Thanks! I feel like I should have thought of this, but I've never actually used hidden fields before, and I was sort of fixating on a solution in PHP itself, instead of using HTML.
PTBNL
Believe me, I know, I've been there more than once. Don't forget to validate all your inputs on the forms AND when you merge the data to avoid injection attacks!!!
Soviut
+2  A: 

Use GET.

In my opinion, POST requests should modify something (e.g. add records to a database). GET requests should retrieve something (e.g. results of a search query).

If you want to use POST anyway, look into PHP sessions.

strager
There is an obvious (and low) size limit on GET data however.
cletus
@cletus, I know, but the OP didn't give any indication as to how much data is needed to be sent between page requests.
strager
+2  A: 

Sessions are a pain, and if you needed them you'd already have implemented them.

As @Soviut said above, hidden input fields are probably the way to go for this.

Matt
Agreed, sessions are viable, but annoying to maintain the correct state for the correct page. Not to mention very resource hungry on the server side with that much data floating around.
Soviut
A: 

If you decide to bite off the session route with the dbms option, I've had luck designing a state class to hold this stuff, and serializing an object using JSON to a single large field in the session record.

le dorfier
+1  A: 

Wez Furlong recently wrote the php5 version on his blog (titled HTTP post from php, without cURL):

function do_post_request($url, $data, $optional_headers = null) { $params = array('http' => array( 'method' => 'post', 'content' => $data )); if ($optional_headers!== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); } return $response; }

In the post he mentions that he always has to look up how to do this. Funny because he's one of the core developers!

Assuming you mean this: http://netevil.org/blog/2006/nov/http-post-from-php-without-curlit looks promising, thanks (although 2006 isn't "recently" to me! :-p) Maybe it's my lack of PHP experience, but I didn't get it to work, and don't see how it would send POST data from one page to another.
PTBNL
A: 

John, any sample code? please send to [email protected] your email will be appreciated. thanks.

Luqman