我制作了一个购物车阵列,如果用户选择了超过1个商品来给他提供cofirm表格,他会在那里输入他们的数据以确认命令,但问题是如果我有超过1个产品,脚本向我显示多个带有sumbit命令的按钮,我想只为命令汇总一个按钮,并将两个项目中的所有日期都插入到mysql db中
cart.php
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
// start select query stmt
$stmt = $con->prepare('SELECT id, product_name, price, size, details FROM products WHERE id = ? LIMIT 1');
$stmt->bind_param('i', $item_id);
$stmt->execute();
$stmt->bind_result($idSelect, $product_name, $price, $size, $details);//variabla pe care o vrei inlocuita prin bind_Result in loc de get_Result
while ($stmt->fetch()) {
$idSelect;
$product_name; // faci acelasi lucru fara $row
$price;
$size;
$details;
}
$stmt->free_result();
// stoop select stmt
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// sumbit the command FORM
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td><center>' . $pricetotal . ' RON</center></td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
$totalProduse = $item_id -1 + $each_item['quantity'];
//trimite form to COFIRMA COMANDA
$cofirmaComanda .='<form action="cofirma_comanda.php" method="post">
<input name="prettotal" type="hidden" value="'. $pricetotal .'">
<input name="produseID" type="hidden" value="'. $item_id .'">
<input name="produseNume" type="hidden" value="'. $product_name .'">
<input name="size" type="hidden" value="'. $my_ArraySize .'">
<input name="cantitate" type="hidden" value="' . $each_item['quantity'] .'">
<input name="produse" type="hidden" value="'. $item_id .'">
<input type="submit" name="CofirmaComanda" value="cofirma_comanda"></form>';
}
表单为$cofirmaComanda
和cofirm命令页面
<?php
$pretTotal = $_POST["prettotal"];
$PRODUSE = $_POST["produseID"];
$produseNume = $_POST["produseNume"];
$size = $_POST["size"];
$cantitate = $_POST["cantitate"];
if(isset($_POST["sumbitDateClienti"])){
$nume = $_POST["nume_client"];
$pretTotalt = $_POST["prettotal"];
$PRODUSE = $_POST["produseID"];
$produseNume = $_POST["produseNume"];
$size = $_POST["size"];
$cantitate = $_POST["cantitate"];
$comanda = 'IDprodus: '.$PRODUSE.' / produseNume: '.$produseNume.' / cantitate: '.$cantitate.' / dimensiune: '.$size.' ';
}
?>
答案 0 :(得分:0)
您想要的是在foreach
循环之外定义表单和提交按钮,并使用数组作为隐藏变量(只需在名称后面添加[]
,在HTML表单中):
$cofirmaComanda = '<form action="cofirma_comanda.php" method="post">';
foreach ($_SESSION["cart_array"] as $each_item) {
// ...
$cofirmaComanda .='
<input name="prettotal[]" type="hidden" value="'. $pricetotal .'">
<input name="produseID[]" type="hidden" value="'. $item_id .'">
<input name="produseNume[]" type="hidden" value="'. $product_name .'">
<input name="size[]" type="hidden" value="'. $my_ArraySize .'">
<input name="cantitate[]" type="hidden" value="' . $each_item['quantity'] .'">
<input name="produse[]" type="hidden" value="'. $item_id .'">';
}
$cofirmaComanda .='<input type="submit" name="CofirmaComanda" value="cofirma_comanda"></form>
然后在cofirma_comanda.php
循环获取您的POST值,逐个处理每个项目:
for ($i=0; $i<count($_POST["prettotal"]); $i++) {
$pretTotal = $_POST["prettotal"][$i];
$PRODUSE = $_POST["produseID"][$i];
$produseNume = $_POST["produseNume"][$i];
$size = $_POST["size"][$i];
$cantitate = $_POST["cantitate"][$i];
// ...
}