简体   繁体   English

PHP中的表单/提交按钮

[英]Form / Submit Button in PHP

This .php file outputs items from an XML file onscreen and gives the user the chance to click on the add to cart submit button which is displayed after each item. 该.php文件从屏幕上的XML文件输出项目,并使用户有机会单击添加到购物车的提交按钮,该按钮显示在每个项目之后。 The idea is that the ID, Name etc are further used in a shopping cart system, but I am still in the starting stages of the shopping cart. 这个想法是在购物车系统中进一步使用ID,名称等,但是我仍处于购物车的起步阶段。 (This is for an assignment) (这是一项任务)

But the problem is that this method does not work since when all the iterations are done, irrespective to which button is clicked, the computer will always refer to the values stored in the variables in the last iteration of the for loop. 但是问题在于该方法不起作用,因为在完成所有迭代后,无论单击哪个按钮,计算机都将始终在for循环的最后一次迭代中引用存储在变量中的值。

I want to write some code in order to be able to have a UNIQUE button for each ID, and when this button is pressed, the corresponding information to that item is put through thi: action="add.php" 我想编写一些代码,以便每个ID都有一个UNIQUE按钮,当按下此按钮时,通过thi放置该项目的相应信息:action =“ add.php”

Any help would deeply be appreciated because I am stuck, and I have no idea 任何帮助将不胜感激,因为我被困住了,我也不知道

<?php
$doc = new DOMDocument();
$doc->load('menu.xml' );

$Wraps = $doc->getElementsByTagName ("Wraps");

foreach ($Wraps as  $w) {
$wrapsid = $w->getElementsByTagName("id")->item(0)->nodeValue;
$wrapsname = $w->getElementsByTagName("Type")->item(0)->nodeValue;
$wrapsprice = $w->getElementsByTagName("Price")->item(0)->nodeValue;

echo "Wraps ID: $wrapsid <br> Wraps Name: $wrapsname <br> Wraps Price:  $wrapsprice";

?>
<form action="add.php" method="post">
                    <td class='view_td' width='10%'><center>
                   <INPUT TYPE=HIDDEN NAME='ID' value='{$wrapsid}'/>
                   <input type='submit' value='Add to cart' /></center></td><br>
</form>
    <?php
    }
    ?>

you can't put form tags around a TD it has to go around the whole table, either use divs and style it to replicate a table layout using CSS or use GET as you are only passing the ID back to the server eg. 您不能将表单标签放在TD周围,而TD必须遍历整个表,要么使用divs并设置其样式以使用CSS复制表布局,要么使用GET,因为您仅将ID返回给服务器。 use an anchor instead of your input 使用锚点代替您的输入

To identify the submitted form, you can use: 要标识提交的表单,可以使用:

•A hidden input field. •隐藏的输入字段。
•The name or value of the submit button. •提交按钮的名称或值。
The name of the form is not sent to the server as part of the POST data. 表单的名称不会作为POST数据的一部分发送到服务器。
more information 更多信息

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

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