Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for passing variables from one HTML page to another?

I'm relatively new to web application programming so I hope this question isn't too basic for everyone.

I created a HTML page with a FORM containing a dojox datagrid (v1.2) filled with rows of descriptions for different grocery items. After the user selects the item he's interested in, he will click on the "Submit" button.

At this point, I can get the javascript function to store the item ID number as a javascript variable BUT I don't know how to pass this ID onto the subsequent HTML page.

Should I just pass the ID as an URL query string parameter? Are there any other better ways?

EDIT: The overall process is like a shopping cart. The user will select the item from the grid and then on the next page the user will fill out some details and then checkout.

I should also mention that I'm using grails so this is happening in a GSP page but currently it only contains HTML.

like image 614
Kevin Avatar asked Oct 19 '08 15:10

Kevin


People also ask

How do you pass a variable from one page to another in HTML?

There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.

What are the two methods of passing data between pages?

Passing variables between pages using URL GET or POST method.


2 Answers

You could just use a hidden input field; that gets transmitted as part of the form.

<html>
  <head>
  </head>
  <body>
    <script type="text/javascript">
      function updateSelectedItemId() {
        document.myForm.selectedItemId.value = 2;
        alert(document.myForm.selectedItemId.value);

       // For you this would place the selected item id in the hidden
       // field in stead of 2, and submit the form in stead of alert
      }
    </script>

    Your grid comes here; it need not be in the form

    <form name="myForm">
      <input type="hidden" name="selectedItemId" value="XXX">
      The submit button must be in the form.
      <input type="button" value="changeSelectedItem" onClick="updateSelectedItemId()">
    </form>
  </body>
</html>
like image 97
extraneon Avatar answered Oct 17 '22 14:10

extraneon


It's good one, but better is to use some script language such as JSP,PHP, ASP....and you can use simple POST and GET methods.

like image 41
vaske Avatar answered Oct 17 '22 12:10

vaske