简体   繁体   English

PHP电子商务页面循环未按预期列出项目

[英]PHP E-Commerce Page Loop Not Listing Items as Expected

I'm building a script which populates a page with products. 我正在构建一个脚本,该脚本将产品填充到页面中。 Under each product is a "Buy" button. 每个产品下方都有一个“购买”按钮。 I looped through the products from the database, and the name and price and all other info turns up correctly. 我从数据库中遍历了产品,然后正确显示了名称和价格以及所有其他信息。 However, when I try to input that same info into the form with hidden fields I only get back the data from the first item from the list. 但是,当我尝试将相同的信息输入到带有隐藏字段的表单中时,我只会从列表中的第一项取回数据。 Let me show you: 让我演示给你看:

<div class="product-foo">
     //Shows up fine
     <p><?php echo product->name; ?></p> 
     <p><?php echo product->price; ?></p>
     <p><?php echo product->info; ?></p>

    //Only first iteration's info gets submitted, no matter which button I click
    <form method="post">
         <input type="hidden" name="product_name" value="<?php echo $product->name; ?>" > 
         <input type="hidden" name="product_price" value="<?php echo $product->price; ?>" >
         <input type="hidden" name="product_info" value="<?php echo $product->info; ?>" >
         <button type="submit" name="finalpost" class="btn btn-primary">Get Product</button>
    </form>
</div>

I thought that each form would be different, so when I clicked on a button on an item only the info for that item would go through, but it's always submitting the first item. 我认为每种形式都会有所不同,因此,当我单击某个项目的按钮时,只会显示该项目的信息,但它始终会提交第一个项目。 I figure it may not work how I'm thinking it should work, but can anyone tell me why this is so and how I can easily resolve this? 我认为它可能无法正常工作,但是有人可以告诉我为什么会这样以及如何轻松解决此问题吗?

Try to use this code for you button: 尝试为您使用此代码按钮:

 <button type="submit" name="finalpost" class="btn btn-primary">Get Product</button>

It looks like you were missing the trailing > after your class definition. 似乎您在类定义之后缺少结尾>。 I suspect the 'view source' of the page would have showed all data, but since you broke the HTML it was being hidden from the browser view. 我怀疑页面的“视图源”会显示所有数据,但是由于破坏了HTML,因此它已从浏览器视图中隐藏了。

Another thing to consider - can you use: 要考虑的另一件事-您可以使用:

  <input type='submit' value='Submit' /> 

instead of: 代替:

 <button> 

to further test? 进一步测试? I suspect this will submit the right form data, but may not be acceptable if your button is calling JS and is doing more than submitting form data. 我怀疑这将提交正确的表单数据,但是如果您的按钮正在调用JS并且比提交表单数据做得更多,则可能无法接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM