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

I am relatively new to programming on web applications, so I hope this question is not too important for everyone.

I created an HTML page with FORM containing dojox datagrid (v1.2) filled with description lines for different products. After the user selects the item you are interested in, he will click the "Submit" button.

At this point, I can get a javascript function to store the element identifier as a javascript variable. But I do not know how to pass this identifier to the next HTML page.

Should I just pass the identifier as a parameter to the URL query string? Are there any other better ways?

EDIT: The general process is like a shopping cart. The user will select an item from the grid, and then on the next page the user will fill in some details and then check.

I should also mention that I use grails, so this happens on the GSP page, but currently it only contains HTML.

+6
javascript html dojo grails
source share
7 answers

You can simply use the hidden input field; which is 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> 
+4
source share

This is good, but it is better to use some script language such as JSP, PHP, ASP .... and you can use simple POST and GET methods.

+2
source share

The best way (imho) is to include it in the url

href = "HTTP: //NewPage.htm variable = value";

encodeUriComponent string Value

+1
source share

One way to send variables using POST to another page is to link to the next input page in a form in which the action attribute is your landing page. For each variable that you have, you can enable the use of inputs of the attribute type "hidden" in this form, making only the button visible.

Another option is to dynamically create links on a page with something like PHP, where you basically re-populate current GET requests.

Finally, you can always store this information in the PHP $ _SESSION array and not worry about constantly transferring these variables through site navigation.

Your choice will depend on how many navigation parameters there are, where you want to keep the same variables. It will also depend on how secure your back is and how much you want to disclose to an advanced web user.

+1
source share

If you only need the identifier on the following pages, you can pass the identifier as a parameter to the query string.

But there will be times when you will need to transfer more information and transfer various parameters to different pages, and to maintain different sets of parameters for different pages, you may get a little hairy. If so, I suggest you keep the hidden field in the form and create an argument object that stores each of your parameters. Serialize the argument object using JSON and save it in a hidden field. Submit the form back to the server. When the next page loads, deserialize the object and retrieve the desired values.

+1
source share

Assuming you're limited to using html pages, I think the best approach would be to pass an identifier along the query string to the next page. It is relatively easy to infer this value from the query string on the next page. If you need to hide a little about passing a variable (or you want the variable to be stored for more than one page), you can also set a cookie and get it on the next page.

0
source share

Since you are trying to do this in a Grails application, you have the choice of using the Flash scope. This may not make any sense if you want to go directly from one HTML page to another, since the area will be defined in the controller. If you don’t need to do any processing between requests, I would suggest using a hidden form field to make it simple.

http://grails.org/Controllers+-+Controller+Scopes

0
source share

All Articles