-1

Am making a booking website, inside it first two pages have forms that are almost same. The point for having the same form is that the user can search again if he wants some different results So how can i transfer form inputs on first page to another form on the next page?. i did try location.search but that gave me some complex input. like if i have selected 18 as my age on first page it should carry it on another. Note: i have similar ids of input-types of those two forms on different webpages, as i have to carry forward some objects.

Dheeraj
  • 19
  • 6

1 Answers1

0

I believe the most direct and easiest way to transfer data from one page to another using only javascript would be for your form to have the method as GET. You can then read the GET data from the url of the page and show / use the data however you please.

Here's a snippet.

Page 1 (page1.html)

<html>
  <head>
    <title>Page 1</title>
  </head>
  <body>
    <form action="page2.html" method="GET">
      <input type="text" name="name" />
      <input type="submit" />
    </form>
  </body>
</html>

Page 2 (page2.html)

<html>
  <head>
    <title>Page 2</title>
  </head>
  <body>
    <span id="data"></span>
    <script type="text/javascript">
      function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
      }
      document.getElementById("data").innerHTML=getParameterByName('name');
    </script>
  </body>
</html>

P.S. I got the getParameterByName function from here

Community
  • 1
  • 1
Sanchit
  • 2,240
  • 4
  • 23
  • 34
  • hey , i have no knowledge of jquery so,this is going out of my head var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"), there is a object related approach to this that am unaware of . – Dheeraj Apr 23 '16 at 18:45
  • I haven't used jQuery at all. FYI If you see code that looks like `$('blah')` or `jQuery('blah')` you know it's most likely jQuery. To answer your question (which will perhaps only further confuse you), that looks like a regular expression that is splitting the url taking into account that there could be multiple parameters and also a possible anchor. – Sanchit Apr 23 '16 at 18:50