问题
Hello I'm hoping this will be an easy fix.
To simply put it, I want to do the following:
1) Have a form that users input data (estimate.html) and submit to validate.php
2) From validate.php, check the POST data and if everything is ok, the user presses the submit button which then sends the same exact POST data to submission.php.
Is there any easy way to send the same exact POST data from the original form throughout my two php files? (Hopefully without sessions)
estimate.html -> validate.php -> submission.php
What I have right now:
I have a form that can have an unknown amount of fields that can be passed into validate.php.
I have already validated all the entries in the POST data sent to validate.php. If all data validates successfully, then rather than inserting all data into hidden fields and resubmitting the data to submission.php, can I just resend the POST that was sent validate.php?
回答1:
Yes! Using session only or hidden fields. I guess..
You can also use array in hidden to reduce the size of elements.
<input type="hidden" value="array('username'=>'user','password'=>'password');">
something like that.
回答2:
You could either move the post data to the session superglobal when it is received in validate.php
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
This will then make that variable available to any subsequent php page run in that session by the client until it is unset.
Alternatively you can use hidden input in the form containing the submit button
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>">
<input type="hidden" name="password" value="<?php echo $_POST['password']; ?>">
<input type="button" ... >
etc
It depends on the scope of where you want this data to be available.
You can also build in the confirmation page into the final page where you simply resubmit to the same page with an extra confirmation post variable asserted which you can check for and proceed if it is.
回答3:
I see two ways of ensuring that the data posted to submission.php
is the same as posted to validate.php
:
You can post data to
submission.php
from withinvalidate.php
using PHP:$data = file_get_contents('php://input'); $options = array('http' => array( 'method' => 'POST', 'content' => $data, ) ); $context = stream_context_create($options); $result = file_get_contents('submission.php', false, $context);
You can return a hash of the POSTed data to the client, whid would be attached to the POST to the
submission.php
. The hash should contain the posted data and a secred key shared byvalidate.php
andsubmission.php
. The latter should then recalculate the hash of the input data to check if the data are validated.
来源:https://stackoverflow.com/questions/16432685/pass-post-data-to-two-php-files