Interrupting $_POST to Preview Input - php

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 code:
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']); ?>
...
Confirm
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!

Related

How to prevent browser's forward button on chrome from submitting a form?

I have a simple php form that submits (POSTS) data on pressing the SUBMIT button and a 'thank you' page is displayed and the data is stored in a database.
Usually on this thank you page if you press the BACK button on the browser and then the FORWARD button on the browser you are brought back to the same thank you page but the form is not submitted again.
In the last few days when I do the BACK and FORWARD on the browser the form resubmits the data and there's a duplicate entry in the database. This happens only in Chrome.
Have I made some errors in the settings in Chrome or is there some other problem somewhere?
The typical solution is known as POST–Redirect–GET. Essentially, your form posts to a page which inserts the data into the database or whatever other actions are necessary and then redirects to another page. That other page doesn't actually do anything but just displays a success message or something. This way, you have two entries in the history: the form and the success page. The form-posting page is never added to the history; pressing back or forward will skip the submission.
Generate a value and put that inside a hidden field. If the user submits the form store that value (must be unique). If one tries to submit the form again with the same generated value, then do not execute your insert or update.
You could set a cookie or session that says the form has already been submitted, and, if that is set don't resubmit the form, but that is basically a band-aid and may not even work...
What you should REALLY be doing is avoiding duplicates by checking the input values against existing values in the db, such as email or username. You should also set your email and username fields to UNIQUE in your database so you'll never get duplicate email addresses or usernames - solving your problem.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')) {
// 1. check if $_POST['email'] already exists in the database
// 2. if email doesn't exist, insert data
}
Th issue is, when you reached the success page and refreshed, the browser has to resubmit the cached data; because the page where it reached is dynamically generated. Now when you click on the okay, the data which was previously stored in the $_POST variable is resubmitted. To stop it, you would have use dynamic binding instead of static binding.
A simple solution to this issue is:
Make the action attribute of the form blank i.e <form action="">.
Call a javascript method onclick of the intended button.
Add the Action attribute in the JS method and submit the form.

php code logic for transferring data from one page to another

i made a (php) form, and used php session and post to transfer the form data to another page, which gives the user an option to have a look at the data and if wrong he shall move to the main form..
now here is what i did
on page1 i made a main form say form1, when user clicks on submit, i saved the data to DB and added that id to a session, and sent that session to next page using the same id
on next page i called from db matching the same id and displayed data accordingly..
now i displayed the data (on the second page) inside a div styled with css, the logic for this second page is giving the user the choice if the data is wrong he can edit, and bottom of this div i added a button with code
Edit
now my question is this, what shall be next? my target is, if user clicks on this edit button he shall be taken to main form (i.e. page1, form1) and there he shall has the option of editing the data in the same id. How can i achieve this friends?
Also i used
<?php session_start(); ?>
on top of both the pages...
Just change your code on the form1 page to; load the data from the database if the id of the record is set in your session variable.
Then if the user decides the data is wrong he just gets sent back to form1 and has the possibility to make changes.
Use something similar to: value="<?php echo $data['name']" in your input fields.
Then check for the id and load the data:
if(isset($_SESSION['record_id'])) {
// Do a query with `where id = record_id
// Fetch the data in the $data variable
}
IF you dont want use GET then use SESSION.
in file.php set
$_SESSION['your_id'] = $theid ;
and in your edit.php retrieve it like that
$id = $_SESSION['your_id'] ;
of course you must session_start in both files.
If you already have all the data in your session you can simply link back to the first page. All you have to do is set all of your fields on the first page to pre-fill with the session data as long as it isset. This way, when you go back the forms will auto fill with the data the user has already entered and they can simply change what is needed.

Store values to database without using form action

I have several web pages created that captures information from user inputs and stores them into session variables in PHP tags inside the html. Finally I echo back the values from the session all on the same page and ask the user if all inputs are correct. I would like to include a submit button at the bottom of the page for the user to submit the session values into the database. I am not sure how to take the values from inside the current page in session variables and store to database. Here is an example of code I currently have.
<?php
session_start();
$name = $_SESSION['name'];
echo "<br>Is this name right?: ".$name;
?>
I have already created the database and respective columns. I am familiar with storing values in form submission, however I have always done this by embedding a file in the form action and obtaining the values using $_POST[''] to get values of inputs of current form.
So, in your queries, just substitute $_POST with $_SESSION. Or am I missing something?

A form with 3 submits; how to keep data for final submit?

I have created a PHP form which requires the user to select a postcode from a list of postcode values.
The user presses submit two times:
- once to go to address select menu which will display a select drop-down with values
- second presses "ok" button to select the address corresponding to his postcode value
I need to keep the value of the selected postcode value for when the form gets submitted. I have tried setting up the postcode drop-down value chosen in a SESSION... but it gets lots when user presses form submit.
How can I keep all the form values even after refreshing the page when the user presses one of the submits?
"How can I keep all the form values even after refreshing the page when the user presses one of the submits?"
Reading your question I didn't understand if each of the form submits actually gets submitted to the server, but I'm going to assume so. I'm also assuming you're trying to use PHP sessions to accomplish this.
When the user submits the form, save the values server-side in a PHP session
//Start the session
session_start();
//Save the values
$_SESSION["foo"] = $_POST["bar"];
...
If, after choosing the address, the user gets redirected to the initial form and you want to populate that:
//Start the session
<?php
session_start();
?>
<!-- Populate HTML form based on previously submitted values -->
<input type="text" name="foo" value="<?php echo $_SESSION["foo"] ?>" />
...
After the final submit you should have all the submitted values saved in the $_SESSION array. Don't forget to always session_start() before trying to handle anything session related.
The short answer is I don't think you can track form data across multiple forms.
I haven't seen your project and so might not fully understand the requirements, but I would suggest you consider using AJAX. Check out the jQuery post() manual, it's really simple actually. This has the advantage of allowing you to update the page once your first form has been completed.
EDIT: Sorry I meant you can't access multiple form data in a single $_POST. Of course you could store it in $_SESSION (remember to start your session properly).

Preserve form data when using history.go(-1)

I have
insert.php
verify.php
in Insert.php I have a form with some fields and some other fields which are generated by ajax calls based on selections. I also have an image uploader where the image is uploaded to the server as soon as the user chooses the image. The preview of the image is then showed when upload is done. When the form is submitted, it sends form.serialize to verify.php.
In verify.php the user sees and verifies the data from insert.php form.
If user thinks that everything is correct then he can submit it. or click on the "Edit" button to go back. When going back some of the info are not available. fields that are generated through ajax are not available.
Anyway to fix it ?
I add element to insert.php using
$('#subcategoryPlaceHolder').html(result);
I'm not sure I understand your app completely, but when working with data like this it's generally a bad idea to go use the back function in the browser.
A better solution would be to save the data that needs to be remembered into the session, and when a user clicks Edit it reloads the form using the data stored in the session, without submitting the form. When a user thinks everything is correct they can submit the form, saving the data, and then you can delete the session data.
I ended up using two divs.
#editDiv and #verifyDiv
using jquery I hid the one I didnt need and the form data was there all the time.

Categories