简体   繁体   中英

What is the better practice to use values from previous HTTP request for ajax request?

Suppose I have page 1 , the user enters some option for selecting data. The data is passed to page 2 , it draws the chart with data from ajax request.

I just wonder what is the better practice to use values from page 1 in page 2. I can come up with 3 approaches

  1. Store value in hidden field, the javascript uses the value of hidden fields for ajax
  2. Embed server side script tag inside javascript for getting the request parameter, such as var data1 = <?php echo $_GET['data1']?>
  3. Generate the javascript needed for page 2 in server side.

The best practice depends on your needs. Your methods work, and may be best for small, quick forms, but problems like load failures, timeouts, script errors and crashes could ruin the session. I would use a server-side database.

The advantage of temporarily storing the Page 1 information in a server database is that you could still have useful information (emails, addresses, etc.) if the user quits the session. This method will also let people pick up where they left off if they have to quit the session. With this method, you don't need to rely on volatile memory or insecure cookies to store information.

  1. Submit form on Page 1 using POST.
  2. Store all form variables in the server database with a unique session key.
  3. Page 2 uses the unique session key to load the stored Page 1 variables in a query.
  4. Submit page 2 including the unique session key.
  5. Combine Page 1 and Page 2 data, permanently insert it into the database, then delete the temporary Page 1 record.

If the user doesn't submit Page 2, delete Page 1 information from the database regularly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM