在php中添加输入字段时更改了dom
order.html.php的代码:在php中添加输入字段时更改了dom,php,html,html-table,html-tableextract,Php,Html,Html Table,Html Tableextract,order.html.php的代码: <body> <p> Place order</p> <table> <thead> <tr> <th>Item No.</th> <th>Name</th> <!--<th>Mrp</th>-->
<body>
<p> Place order</p>
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<form id="form1" action="" method="post" >
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<div><input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
<div>
</tbody>
</form>
</table>
</body>
下单
项目编号。
名称
价格
量
尝试正确的html结构:
<body>
<p> Place order</p>
<form id="form1" action="" method="post" >
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<!--<td><?php echo $productData['mrp']; ?></td>-->
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="4">
<input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
</td></tr>
</tbody>
</table>
</form>
</body>
下单
项目编号。
名称
价格
量
- 首先,只需用
标记将表包装起来
- 其次,使用
productId
作为name=“$productId”
,实际上并不好,相反,可以使用类似name=“products[$productId]”的方法,以便在提交表单后,将它们分组到与数组相同的变量上
示例代码:
指数作为键,值是每个数量乘以价格的总和
您的HTML无效,这就是.HTML有效的原因。现在看@thinkNo它不是@devprashant。查看你的HTML>>>你不能把表单标签放在AD之后,你不能把div放在关闭TRD之后。谢谢。这对我来说是件新鲜事@我和以前一样。上面有人给出了背后的原因。@devprashant什么原因?
<body>
<p> Place order</p>
<form id="form1" action="" method="post" >
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<!--<td><?php echo $productData['mrp']; ?></td>-->
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="4">
<input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
</td></tr>
</tbody>
</table>
</form>
</body>
$dataProduct = array(
array('productId' => 1, 'productName' =>'hp keyboard old', 'productPrice' => 400),
array('productId' => 2, 'productName' =>'lenovo keyboard old', 'productPrice' => 430),
array('productId' => 9, 'productName' => 'kaspersky antivirus', 'productPrice' => 430),
);
if(isset($_POST['submit'])) {
$values = $_POST['products']; // get all the grouped values
$total = array();
foreach($values as $productId => $quantity) { // loop values
foreach($dataProduct as $productData) {
if($productData['productId'] == $productId) {
// simple multiplication, quantitiy times price for each item
$total[$productId] = $productData['productPrice'] * $quantity;
}
}
}
echo '<pre>';
print_r($total);
echo '</pre>';
}
?>
<form method="POST">
<table cellpadding="10">
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ; foreach($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="products[<?php echo $productData['productId']; ?>]"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td><input type="submit" name="submit" /></td>
</tr>
</tbody>
</table>
</form>
Array
(
[1] => 800
[2] => 860
[9] => 860
)