2

I'm currently designing a system working with steps (like an airline booking, where you pass on various steps to buy a ticket) and these steps are being done via AJAX. So, the index.php file is the sole file loaded and the pages' are being loaded via AJAX on the main div.

The system is working as it should be, but I need to ensure the variables' passing on the subsequent loaded pages, based on the data entered before. E. g.:

I have a form on page 1 where the user enter some data, like his name and country. When clicking submit, the button triggers an AJAX request where the result is the content of page 2, being loaded on the div. But I need that the page 2 have the variables filled with the values on page 1. Page 2 have another form with new data and another submit button. When clicked, it requests via AJAX the page 3 which have to contain the variables at pages 1 and 2. And so on.

Which is the smartest way to do that? Using PHP session? Passing via $.get or $.post?

I request an opinion of you.

Thank you very much.

6
  • if you are using the same page but different data for each page and is using ajax, why don't you just store the values submitted each time in js variables and use it subsequently? a more pertienent question would be, how you are implementing ajax here? using any js library? Commented Apr 12, 2013 at 13:16
  • I'm using jQuery. But I cannot store those variables on js because* on one step there's a huge treating of that data, working with a database and multiple HTTP and XML requests to parse a final data. Since I'm using PHP for that matter (and the processing tool is already done), using PHP would do* my service quickly. Commented Apr 12, 2013 at 13:20
  • so page2 data depends on the processing being done on the page1 data in the backend and not just the page1 data directly right? if that is so, you could prepare the necessary data in the php script, pass it back to the ajax call as response (you could use json to handle the data), and then use js to prepare the fields for page2 and display it... Commented Apr 12, 2013 at 13:24
  • I need both: the page2 needs the data direcly entered on page1 for the processing issues, but the page2 have to be displayed at main div, using the data entered and the data processed based on the entrance made on page1. I'm trying to use AJAX to make a more presented and smart page for the user, avoiding to the page to be reloaded every time. The system is working fine using PHP only, where I'm passing the variables using $_POST on hidden fields. The idea is: also pass the variables to the subsequent pages, to be processed and shown by them, without leaving the main index page. Commented Apr 12, 2013 at 13:31
  • 1
    for the processed values from php script to be used on page2, you can use json to pass it back to the original ajax call and then populate it on page2 via JQuery.. see yossarian's answer below.. Commented Apr 12, 2013 at 13:40

2 Answers 2

8

If you are using a library like jQuery - the most easy option is to pass the variables as POST or GET parameters, see: http://api.jquery.com/jQuery.ajax/

For example:

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
  }).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

You can change the type to be GET or POST depending on your needs and the data key should hold the data you wish to send. (in JSON format)

On how to pass the php data as JSON to the js, see passing php variable

Sign up to request clarification or add additional context in comments.

Comments

1

I would go for the

$.post

option of jQuery. http://api.jquery.com/jQuery.post/ Pass all the variables to your PHP script, and let that PHP script generate the new content of the div (next step) with all the variables (new ones + the ones from the post) filled in. If there are variables that should be transferred but not shown to the visitor, use hidden fields to transfer them. BTW: to get the contents of all the inputs in your form via jQuery, use

serialize()

http://api.jquery.com/serialize/ They work together like a charm!

2 Comments

hidden fields are not displayed on the browser, but an user can still view and alter them if needed..
If there is information that should not be "alterable", you can still set that in a session variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.