将变量从一个HTML页面传递到另一个HTML页面的最佳实践是什么?

时间:2022-11-28 08:45:07

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

我对Web应用程序编程比较陌生,所以我希望这个问题对每个人来说都不是太基础。

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.

我创建了一个带有FORM的HTML页面,其中包含一个dojox数据网格(v1.2),其中填充了不同杂货项目的描述行。用户选择了他感兴趣的项目后,他将点击“提交”按钮。

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.

此时,我可以使用javascript函数将项目ID号存储为javascript变量但我不知道如何将此ID传递到后续HTML页面。

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

我应该只将ID作为URL查询字符串参数传递吗?还有其他更好的方法吗?

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.

我还应该提一下,我正在使用grails,所以这发生在GSP页面中,但目前它只包含HTML。

7 个解决方案

#1


4  

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>

#2


2  

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.

这是一个很好的,但更好的是使用一些脚本语言,如JSP,PHP,ASP ....你可以使用简单的POST和GET方法。

#3


1  

The best method (imho) is to include it in the URL

最好的方法(imho)是将它包含在URL中

href="http://NewPage.htm?var=value";

encodeUriComponent a string Value

encodeUriComponent是一个字符串值

#4


1  

One way to send over variables using POST to another page is to make the link to the subsequent page a submit input on a form where the action attribute is your target page. For every variable you have, you can include using inputs of attribute type "hidden" in this form, making only the button visible.

使用POST将变量发送到另一个页面的一种方法是将指向后续页面的链接作为action属性为目标页面的表单上的提交输入。对于您拥有的每个变量,您可以在此表单中使用属性类型“隐藏”的输入,仅使按钮可见。

Another option is to dynamically generate links on the page with something like PHP where you basically repopulate the current GET queries.

另一种选择是在页面上动态生成链接,例如PHP,您基本上可以重新填充当前的GET查询。

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

最后,您始终可以将此信息存储在PHP $ _SESSION数组中,而不必担心通过站点导航不断传递这些变量。

Your choice will depend on how many navigational options there are where you'd like to keep the same variables. It will also depend on how secure you'd like your back end to be and the amount you'd like to disclose to the advanced web user.

您的选择取决于您希望保留相同变量的导航选项数量。它还取决于您对后端的安全程度以及您希望向高级网络用户披露的金额。

#5


1  

If you are only going to need the ID on the subsequent pages, then you can pass the id as a query string parameter.

如果您只需要在后续页面上使用ID,则可以将id作为查询字符串参数传递。

But there will be times when you need to relay more information and passing a variety of parameters to different pages and having to maintain different sets of parameters for different pages can get a little hairy. When this is the case I'd suggest that you keep a hidden field on the form and create an argument object that stores each of your parameters. Serialize the argument object with JSON and store this in you hidden field. Post the form back to the server. When the next page loads, deserialize the object and retrieve the values you need.

但有时你需要传递更多信息并将各种参数传递给不同的页面,并且不得不为不同的页面维护不同的参数集可能会有点毛茸茸。在这种情况下,我建议您在表单上保留一个隐藏字段,并创建一个存储每个参数的参数对象。使用JSON序列化参数对象并将其存储在隐藏字段中。将表单发回服务器。加载下一页时,反序列化对象并检索所需的值。

#6


0  

Assuming that you are limited to using html pages, I think the best approach would be to pass the id along on the query string to the next page. It is relatively easy to pull that value back off the query string on the next page. If you need to be a little more stealthy about passing the variable (or you need the variable to persist for more than one page), you could also set a cookie and retrieve it on the next page.

假设您仅限于使用html页面,我认为最好的方法是将查询字符串中的id传递给下一页。将该值从下一页的查询字符串中拉回来相对容易。如果你需要更加隐秘地传递变量(或者你需要变量持久存在多个页面),你也可以设置一个cookie并在下一页上检索它。

#7


0  

Since you are trying to do this in a Grails application you do have a choice of using Flash scope. This might not make any sense if you want to go directly from one HTML page to the next as the scope would be defined in a controller. If you do not need to do any sort of processing between requests, I'd suggest using a hidden form field to keep it simple.

由于您尝试在Grails应用程序中执行此操作,因此您可以选择使用Flash范围。如果您想直接从一个HTML页面转到下一个HTML页面,这可能没有任何意义,因为范围将在控制器中定义。如果您不需要在请求之间进行任何处理,我建议使用隐藏的表单字段来保持简单。

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

#1


4  

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>

#2


2  

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.

这是一个很好的,但更好的是使用一些脚本语言,如JSP,PHP,ASP ....你可以使用简单的POST和GET方法。

#3


1  

The best method (imho) is to include it in the URL

最好的方法(imho)是将它包含在URL中

href="http://NewPage.htm?var=value";

encodeUriComponent a string Value

encodeUriComponent是一个字符串值

#4


1  

One way to send over variables using POST to another page is to make the link to the subsequent page a submit input on a form where the action attribute is your target page. For every variable you have, you can include using inputs of attribute type "hidden" in this form, making only the button visible.

使用POST将变量发送到另一个页面的一种方法是将指向后续页面的链接作为action属性为目标页面的表单上的提交输入。对于您拥有的每个变量,您可以在此表单中使用属性类型“隐藏”的输入,仅使按钮可见。

Another option is to dynamically generate links on the page with something like PHP where you basically repopulate the current GET queries.

另一种选择是在页面上动态生成链接,例如PHP,您基本上可以重新填充当前的GET查询。

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

最后,您始终可以将此信息存储在PHP $ _SESSION数组中,而不必担心通过站点导航不断传递这些变量。

Your choice will depend on how many navigational options there are where you'd like to keep the same variables. It will also depend on how secure you'd like your back end to be and the amount you'd like to disclose to the advanced web user.

您的选择取决于您希望保留相同变量的导航选项数量。它还取决于您对后端的安全程度以及您希望向高级网络用户披露的金额。

#5


1  

If you are only going to need the ID on the subsequent pages, then you can pass the id as a query string parameter.

如果您只需要在后续页面上使用ID,则可以将id作为查询字符串参数传递。

But there will be times when you need to relay more information and passing a variety of parameters to different pages and having to maintain different sets of parameters for different pages can get a little hairy. When this is the case I'd suggest that you keep a hidden field on the form and create an argument object that stores each of your parameters. Serialize the argument object with JSON and store this in you hidden field. Post the form back to the server. When the next page loads, deserialize the object and retrieve the values you need.

但有时你需要传递更多信息并将各种参数传递给不同的页面,并且不得不为不同的页面维护不同的参数集可能会有点毛茸茸。在这种情况下,我建议您在表单上保留一个隐藏字段,并创建一个存储每个参数的参数对象。使用JSON序列化参数对象并将其存储在隐藏字段中。将表单发回服务器。加载下一页时,反序列化对象并检索所需的值。

#6


0  

Assuming that you are limited to using html pages, I think the best approach would be to pass the id along on the query string to the next page. It is relatively easy to pull that value back off the query string on the next page. If you need to be a little more stealthy about passing the variable (or you need the variable to persist for more than one page), you could also set a cookie and retrieve it on the next page.

假设您仅限于使用html页面,我认为最好的方法是将查询字符串中的id传递给下一页。将该值从下一页的查询字符串中拉回来相对容易。如果你需要更加隐秘地传递变量(或者你需要变量持久存在多个页面),你也可以设置一个cookie并在下一页上检索它。

#7


0  

Since you are trying to do this in a Grails application you do have a choice of using Flash scope. This might not make any sense if you want to go directly from one HTML page to the next as the scope would be defined in a controller. If you do not need to do any sort of processing between requests, I'd suggest using a hidden form field to keep it simple.

由于您尝试在Grails应用程序中执行此操作,因此您可以选择使用Flash范围。如果您想直接从一个HTML页面转到下一个HTML页面,这可能没有任何意义,因为范围将在控制器中定义。如果您不需要在请求之间进行任何处理,我建议使用隐藏的表单字段来保持简单。

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