Interrupting $_POST to Preview Input
I have set up a database and have form data being submitted to it via $_POST in PHP, and I also have my page set up and working which pulls in the fields from the database and displays them. Not rocket science I know.
However what I want to do now is place a page inbetween the submission of the form and the insert in the database, to give the user a chance to check their entry.
I have created this page and used the $_POST data to display the data from the form (as it has not been entered into the database yet), however i'm wondering how, if the user approves the submission, I then INSERT it into the database. (I've tried running the INSERT query from the $_POST data on pressing a submit button, however because (I assume) ivew interrupted the flow between the original form submission and the INSERT query, all I get is a list of errors for unrecognised variables.
So what I have is this process:
form.php /user enters info using $_POST
preview.php /user is previewed info using $_POST and Session code starts (below)
submit.php /MySql query runs but returns all errors for undefined indexes
This is my session start c开发者_如何学Pythonode:
session_start();
if (isset($_POST['preview'])) {
$_SESSION['company_name'] = $_POST['company_name'];
etc etc - remaining form field names
}
POST data is not automatically passed on to other pages. Save the submitted data in the session and read it from there after the confirmation page, or insert all that data back into the page using <input type="hidden">
elements, so they can be resubmitted as POST.
A very simple illustration of the process using sessions:
form.php
<form action="confirm.php" ...>
...
</form>
confirm.php
<?php
session_start();
$_SESSION['data'] = $_POST;
?>
Please confirm:
Name: <?php echo htmlspecialchars($_POST['name']); ?>
...
<a href="save.php">Confirm</a>
save.php
<?php
session_start();
$data = $_SESSION['data'];
// save $data to database
You should do it with 3 modes
- Display
- Verify
- Insert
The quick an easy way is just to have a GET parameter or a hidden field that says the last state of the form. First time you display it its set to DISPLAY
. Form submits, your script looks at the field, knows that after DISPLAY
comes VERIFY
, and presents the verification page. User submits, script looks at the field, knows that after verify you insert into the database, and you preform the relevant query.
Not sure how you were doing it before, but I assume that you were redirecting or just having the user click a link, which lost all the $_POST data
Unless I'm misunderstanding what you're saying/doing, I'd do something like this:
When the user clicks the "save" (or whatever) button, hijack that click event and use the form data to do the preview business. When they click "confirm", have that send the data to the server and save it to the database.
Again, I'm not 100% sure if that will work, but perhaps it's a place to start.
Good luck!
精彩评论