多个表单或多个页面提交? [英] Multiple Forms or Multiple Submits in a Page?

查看:155
本文介绍了多个表单或多个页面提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用网站上销售的产品创建一个页面。我想在每个产品附近添加一个添加到购物车按钮,这些按钮的标记类似于此:

  < h4 class =productHeading>产品名称1< / h4> 
< div>
有关产品的额外信息1.
< / div>
< h4 class =productHeading>产品名称2< / h4>
< div>
有关产品的额外信息2.
< / div>
...

由于提交输入将具有不同的名称包括产品),最大的问题是:我是否应该将整个产品列表包装在一个表单中,还是应该为每个产品创建一个表单?在代码中:

 < form method =postaction =process.php> 
< h4 class =productHeading>产品名称1< / h4>
< div>
有关产品的额外信息1.
< input type =submitname =submit1value =Add to Cart>
< / div>
< h4 class =productHeading>产品名称2< / h4>
< div>
有关产品的额外信息2.
< input type =submitname =submit2value =Add to Cart>
< / div>
< / form>

或...

 < h4 class =productHeading>产品名称1< / h4> 
< div>
有关产品的额外信息1.
< form method =postaction =process.php>
< input type =submitname =submit1value =加入购物车>
< / form>
< / div>
< h4 class =productHeading>产品名称2< / h4>
< div>
关于产品的额外信息2.
< form method =postaction =process.php>
< input type =submitname =submit2value =加入购物车>
< / form>
< / div>

哪一个是最佳做法?任何严重的原因,不使用一个或另一个,或者我做它完全错误?

解决方案

每种产品的一种形式绝对是最好的选择。



优点:$ b​​
$ b


  • 它可以节省您解析数据的麻烦,以确定哪个产品被点击

  • 它会减少发布数据的大小。






在您的具体情况中

如果您只打算使用一个表单元素,在这种情况下 submit 按钮,所有表单都应该可以正常工作。






我的推荐
每个产品都做一个表单,并将您的标记更改为:

 < form method =postaction => 
< input type =hiddenname =product_idvalue =123>
< button type =submitname =actionvalue =add_to_cart>添加到购物车< / button>
< / form>

这会给你一个更清洁和可用的 POST 。没有解析。 它可以让你在将来添加更多的参数(大小,颜色,数量等)。 b
$ b


注意:使用<按钮> <输入> ,但是作为程序员,我发现使用 action =='add_to_cart' action =='Add to Cart' code>。另外,我讨厌把表达与逻辑混合在一起。如果有一天你决定按钮说添加更有意义,或者如果你想使用不同的语言,你可以自由地做到这一点,而不必担心你的后端代码。



I'm creating a page with the products sold in the website. I'd like to include an "add to cart" button near each product, which are listed with markup similar to this:

<h4 class="productHeading">Product Name 1</h4>
<div>
  Extra information on the product 1.
</div>
<h4 class="productHeading">Product Name 2</h4>
<div>
  Extra information on the product 2.
</div>
 ...

Since the submit inputs will have different names (with the code of the product included), the big question is: should I wrap the entire product list in a form, or should I create one form for each product? In code:

<form method="post" action="process.php">
  <h4 class="productHeading">Product Name 1</h4>
  <div>
    Extra information on the product 1.
    <input type="submit" name="submit1" value="Add to Cart">
  </div>
  <h4 class="productHeading">Product Name 2</h4>
  <div>
    Extra information on the product 2.
    <input type="submit" name="submit2" value="Add to Cart">
  </div>
</form>

Or…

<h4 class="productHeading">Product Name 1</h4>
<div>
  Extra information on the product 1.
  <form method="post" action="process.php">
    <input type="submit" name="submit1" value="Add to Cart">
  </form>
</div>
<h4 class="productHeading">Product Name 2</h4>
<div>
  Extra information on the product 2.
  <form method="post" action="process.php">
    <input type="submit" name="submit2" value="Add to Cart">
  </form>
</div>

Which one is best practice? Any serious reason not to use one or the other, or am I doing it completely wrong?

解决方案

Best practice: one form per product is definitely the way to go.

Benefits:

  • It will save you the hassle of having to parse the data to figure out which product was clicked
  • It will reduce the size of data being posted

In your specific situation

If you only ever intend to have one form element, in this case a submit button, one form for all should work just fine.


My recommendation Do one form per product, and change your markup to something like:

<form method="post" action="">
    <input type="hidden" name="product_id" value="123">
    <button type="submit" name="action" value="add_to_cart">Add to Cart</button>
</form>

This will give you a much cleaner and usable POST. No parsing. And it will allow you to add more parameters in the future (size, color, quantity, etc).

Note: There's no technical benefit to using <button> vs. <input>, but as a programmer I find it cooler to work with action=='add_to_cart' than action=='Add to Cart'. Besides, I hate mixing presentation with logic. If one day you decide that it makes more sense for the button to say "Add" or if you want to use different languages, you could do so freely without having to worry about your back-end code.

这篇关于多个表单或多个页面提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆