Form submission preview with form validation (PHP) - php

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

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.

Integrated Login Form Going in circles

So I am building this domain on a seperate URL for right now, but that shouldnt effect a login coding. Everytime I click "login" it just cycles the current page.
Here is my code
What Am I missing or doing wrong? Reset Password works perfect.
Thanks,
Jeff Gray
You're not setting the action attribute in the form. If you submit a form without an action attribute, or an empty value for the action attribute, the form's action becomes the current page (also known in PHP as PHP_SELF)
<form action="PAGE_WHERE_IT_IS_SUPPOSED_TO_GO" ...>
</form>
If, by chance the form is supposed to have the same action as the current page, then the code provided above is not sufficient enough to handle the request; as you would need to parse the form (above any HTML output), complete your server time validation, and determine if you needed to keep them on the same page (insufficient/incomplete data) or redirect the user to the next stage of your site.

PHP: How to store temporary POST data before submit?

I'm developing an application in mobile view using CakePHP, where there's no Javascript code can run.
My scenario:
In user login form, users clicked submit button. But before the data submitted and users go to the next page, I make an 'interruption' page (renders a new view). In there, contains "Facebook" and "No, thanks" button. When they click "Facebook", they will connect their Facebook accounts. But, if they click "No, thanks", the login continue.
My question is, how can I make that "No, thanks" button? Because if I use <input type="submit"> button, the form that contain its data is in previous page, so this button will not be clicked. And, how to store POST data, and when that "No thanks" button clicked, then, the data submitted?
You can either store the data in session, or you can make the "no thanks" button the submit button of a form where all the data from the previous page is stored in hidden form fields.
Personally for a number of reasons, I would prefer to utilize sessions, but since you are even asking this question, I am guessing you are unfamiliar with usage of session data.
A simpler question would be:
To make these two forms on single page but with LOTS of vertical or horizontal space in between user login form & 'interruption' page so user seeing the login form can't see interruption section although both are within same <form></form> tag. Then, the the submit button is simply a link to interruption section of the same form. this link is simple an <a> tag with inline linking, which would hide the login form & display interruption page without needing javascript. similar to links to Go to Test Section A on this page
http://www.dynamicdrive.com/dynamicindex5/bookmarkscroll.htm
Further then in the interruption section you can use a real submit button which makes a POST back to request the server.
[edit]
This workaround is based on assumption that user doesn't scroll far enough. Because most users won't, making it 1step process for them instead of 2process which is specially important on mobile. And if somebody does then the empty form gets submitted to the server & then we can run validation & return a error to the user. where he/she can fill the complete form. so This is a good workaround better than storing the data in session & making two POST request for every user login over mobile network which are generally unreliable.
A better solution might be just to combine both forms into one form. Then you don't have to make 2 http requests. Maybe all you need to do is to add one extra button to the original form "Login with FaceBook" next to the other submit button.

Registration form - Checking if data entered ($_SERVER['PHP_SELFf'])

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.

Multi page form validation with php

This is my first post, so be kind ; )
I'm wanting to create a multi page form with php.
The form will spread over 3 pages, each page will need to validate on the data entered into the form on the client (using jquery validation) and if javascript is disabled, on the server, where error messages need to be displayed beside the related form field.
Upon validation, the data needs to be passed to the next page in the form, preferable using session variables.
The main problem I'm having is that most validation scripts now leave the action="" as being self referring to the current page, and as such post variables cannot be passed onto a different page in the chain of forms.
I want to have a validation script that will validate, and then post to a new page upon clicking the submit button.
Thanks
Peter
You don't have to post to the next page.
You can validate the form fields on the current page, store them in a session, then use a header("location: nextPage.php"); exit(0); redirect to go to the next page.
generally, you can do something like
<form onsubmit="return validateForm();" method="post" action="/wherever">
And this will call your javascript validation form, not submitting the form if the validation form returns false.
You will also need to do server side validation, and I suggest that you store the previous forms validated results into the session, rather than re-posting them (as these wont have to then be re-validated each time!)
You should use the post redirect pattern. Post the variables to the next page (control page), If validations pass then that page does a
header("Location: /page2.php");
after saving the posted variables to the session. If the server side validation fails, then you do a header to the page1.php with the error. pThis way you can use your back button.
I reccommend this JS validator. Very easy to use and doesn't depend on the action="" parameter at all, so you can still set it to whatever you want.

Categories