tags:

views:

62

answers:

2

Hi all.

I have many forms that use AJAX (w/ jQuery) for validation and data submission. When a form is filled out correctly, I use window.location to redirect the page after I get an acceptable response from the PHP script. On the new page, I use a session variable (set after the AJAX calls) to display the appropriate content. Please tell me if this is standard practice or please give me some suggestions.

Thanks!

+1  A: 

Is there a reason you would use a $_SESSION variable to store the post-submission content? Standard practice would be to validate the form via AJAX but submit it in the standard way (i.e. via $_GET or $_POST) after validation. This way you don't need to store anything to a session and you'll likely have less to debug as you'll be submitting the form and displaying its results in the most widely-accepted way.

Evan Hanson
Additionally, it'll still work on browsers that don't support Javascript.
MSpreij
I should have been more clear. The form processor script that is called via ajax, processes the POST values and inserts them into the db. After insert, it puts the insert id in a session and returns an 'ok' to the requesting page. The requesting page does a window.location callback and directs the user to a new page. The new page does a query with the id that is stored in a session and displays information. BUT, your suggestion is great. Thanks!
pistolshrimp
The session still seems unnecessary; you could return the insert id to a form field with the AJAX call and retrieve it from the submitted form data to display on the results page. It's nice to keep all your data in one place (in this case, the $_GET or $_POST array).
Evan Hanson
A: 

The benefit of AJAX is typically so that you can submit the form without actually having to do the redirect/refresh. You could get the same functionality by simply having your form POST to the destination URL, redirect to the appropriate place from there or send them back to the form displaying any errors that may have occurred. You could use AJAX to validate the form before the submission to save your users a redirection back to the form to fix their errors, but this is really just a convenience for them. Also, you will have to validate any user data on the server side once it has been submitted, as you can't rely on client-side validation, so you might as well forget the AJAX validation.

Graham