BradStevenson
BradStevenson

Reputation: 1994

Session cookies and HTML forms

I have a HTML page containing a form which consists of; text, check boxes and drop-down menus.

on completion the user clicks submit and is taken to a new page, on this page I have a JavaScript which performs some analysis on the inputs and then writes its results.

THE PROBLEM: transferring the form inputs to the next page, I need to be able to access the form inputs on the next page,

e.g if text input was "true" I can access this on the next page and assign it into a variable.

So at the moment I'm thinking the best way to do this is with a session cookie.

However I'm struggling with the code for storing all the form inputs into the cookie and then how to access them on the second page.

Any help would be greatly appreciated! or any ideas on other methods to complete this task!

Thanks!

Upvotes: 2

Views: 1013

Answers (3)

Colin Ross
Colin Ross

Reputation: 589

If you don't need to do anything server-side, you can submit the form using the method='GET' rather than POST. That way, the inputs will be available in the url and can be parsed in your JavaScript.

For example:

<html>
  <script type='application/javascript'>
    var search = location.search;
    var parts = search.slice(1).split('&');    // Need to slice to remove initial '?'
    for (var i=0; i < parts.length; ++i)
    {
      var info = parts[i].split('=');
      var variable = info[0];
      var value = info[1];
      document.write("<p>" + variable + " = " + value + "</p>");
    }
  </script>
  <form method='GET'>
    <input type='text' name='name'>
    <input type='checkbox' name='chosen'>
    <input type='submit' value='Go'>
  </form>
</html>

Upvotes: 2

reevesy
reevesy

Reputation: 3472

A quick google came up with this http://www.fijiwebdesign.com/blog/remembering-form-input-data-with-javascript-and-cookies-before-submission.html

It should give a good idea of how to do it

Upvotes: 1

Gregor
Gregor

Reputation: 4434

You can encode your Data inside an serialized Object (JSON) or using urlencoded strings (like var1=test&var2=true). Store this string into a cookie and you can use it on the next page.

Upvotes: 0

Related Questions