Some one should please guide me on how i can retrieve forms data after i redirect a user back to the page of the form due to error occurrence.
In some website registration page i observe that when i submit the form and it happens that my data contains error, i will be redirect back to the registration page displaying the error message but the data that i fill early will still appear on the input boxes, only the input box that contain error will be empty, please i don't know how to do this on site i only know how to redirect and display error message.
All POST data will be lost after a redirect. If you really want to save those form values, you will need to store them as session variables. Example:
$_SESSION['form']['name'] = $_POST['name'];
$_SESSION['form']['email'] = $_POST['email'];
Check out this page for some basic session-usage.
You can use sessions or cookies to save the values.
or you can use javascript to validate your form inputs. So the page won't reload. so datas will be retained.
you can submit values to same page (same page which include form)
and check if informations is submitted using:
if(isset($_POST['data'])){
//validate
}else{
//display form
}
Related
This is going to sound like a dumb question, so I apologize upfront.
I have a PHP form, with its action posting to itself, so that I can do some validation to make sure text boxes, a few checkboxes and a couple radio buttons are selected. After I confirm there are no form errors, I need to let the user preview what they've entered before letting them submit the information to a database.
For previewing form submission details, logically page1.php would have <form action="page2.php"> and page2.php would allow the user to preview what they've submitted.
But for form validation, logically, the form should submit to itself so that it can validate all required fields are entered while on the same page.
So, is there a way that I can direct the user to a "next page" after the form has validated by submitting to itself, so that they can preview the information?
Note:
I am rewriting a classic ASP form, which does this:
if errorMsg = "" then
response.redirect "verify.asp"
else
session("errorMsg") = errorMsg
response.redirect "default.asp"
end if
I assume in the case of PHP, the "else" portion of a similar conditional would be unnecessary since it already posts to itself, it's only when the error message array is empty that it should redirect to the verify page.
You can use header to perform a redirect:
if(empty($errorMsg)) {
header("Location: verify.php");
exit;
}
Using Mark Heintz's answer and some googling, I figured out the best way to do it is with a combination of Javascript and a meta tag. The meta tag will account for when javascript is disabled.
Redirect in PHP without use of header method
Redirecting to a relative URL in JavaScript
There is also an AJAX way of doing it: Modify the URL without reloading the page
I have a form on one php page. Input values are submitted to another php page where they are processed. When values are incorrect or missing I go back to the form using:
header('Refresh: 3; url=biertjetoevoegen.php;')
The problem is that at this time the $_POST variables are not SET anymore, so the user has to re-enter all the data....
How can I go back to the form without losing the $_POST variables, not using $_SESSION variables?
If you want to redirect user to another page that has posted data to, you have to send data yourself to that page (by $_SESSION or $_GET or any other ways), but you should not do it yourself.
The best way is to set your action property of your form to be the same as your html form page and the page that you validate data (page that contains your php code).
Note that all PHP frameworks do this.
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.
What I want to do is,
User comes to registration form.
Fills it , press enter, registration goes on.
If he doesnt fill it, echo Data Missing.
Where I am stuck,
Using isset, it echos Data Missing at the first visit itself which shouldnt happen.
Any suggestions?
I am not looking for any codes as I want to learn it up myself. Just the idea would do.
On the first form load, check the contents of $_SERVER['REQUEST_METHOD']
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Verify the form has been filled in
// If any are missing, display missing data message
// and display the form again
}
else {
// Just display the form for the first time
}
I also recommend reading up on the post-redirect-get pattern, wherein you display the form from a script which does no form processing itself. The form posts to another handler script (rather than back to itself), which does all the validation and action, and finally redirects to a new location or back to the original form on error or incomplete data.
Read more about the $_SERVER superglobal array.
Can you put the "action" code in another page?
That's what I normally due, mostly to prevent the user from resubmitting the data on a page refresh, but it would also resolve your issue.
Alternatively, you could take into consideration PHP upload - Why isset($_POST['submit']) is always FALSE in which they make some valid points about the isset function.
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!