我檢查了許多不同的問題,但幾乎所有問題都即將發布復選框值。我要做的是只發布選中復選框的行。
表單內容從另一個 PHP 檔案中使用 AJAX 查詢 3 個連接表中檢索出來。這是為了通過下拉選擇提供實時更新以過濾客戶的結果。
$sql = "SELECT * FROM z_sales_consigne AS a LEFT JOIN z_sales_price AS b ON a.SKU = b.SKU LEFT JOIN z_sales_discount AS c ON c.CustomerID = a.CustomerID WHERE a.CustomerName = '".$_POST["CustomerName"]."'";
表單.php
<form name="SalesAdd" action="sls-calc.php" method="post" enctype="multipart/form-data" id="porequest"/>
<td width="30px" style="text-align:center;">
<input type="text" name="item_price[]" id="item_price" value="'.$row["price"]. '"/>
<input type="text" name="item_rate[]" id="item_rate" value="'.$row["rate"]. '"/>
<input type="text" name="item_discount[]" id="item_discount" value="'.$row["Discount"]. '"/>
<input type="checkbox" id="addProduct" name="Product[]" value="'.$row["id"]. '">
</td>
<button type="submit" name="save" id="save" class="btn btn-info" >
</form>
發布 sls-calc.php
foreach($_POST['Product'] as $key=>$check) {
echo "<tr>";
echo "<td width='120px'>".$_POST["item_price"][$key]. "</td>";
echo "<td width='400px'>".$_POST["item_rate"][$key]. "</td>";
echo "<td width='90px'>".$_POST["item_discount"][$key]. "</td>";
echo "</tr>";
}
帶有選擇的原始表單行:

當我提交時;結果從第一行開始,與選中的復選框數量一樣多。
但結果應該包含選定復選框行的資料
我應該怎么做才能只獲取選定復選框的行?


編輯
array (size=6)
'item_price' =>
array (size=7)
0 => string '112,16' (length=6)
1 => string '112,16' (length=6)
2 => string '254,87' (length=6)
3 => string '254,87' (length=6)
4 => string '254,87' (length=6)
5 => string '254,87' (length=6)
6 => string '254,87' (length=6)
'item_rate' =>
array (size=7)
0 => string '8' (length=1)
1 => string '8' (length=1)
2 => string '8' (length=1)
3 => string '8' (length=1)
4 => string '8' (length=1)
5 => string '8' (length=1)
6 => string '8' (length=1)
'item_discount' =>
array (size=7)
0 => string '20' (length=2)
1 => string '20' (length=2)
2 => string '20' (length=2)
3 => string '20' (length=2)
4 => string '20' (length=2)
5 => string '20' (length=2)
6 => string '20' (length=2)
'productChk' =>
array (size=2)
0 => string 'on' (length=2)
1 => string 'on' (length=2)
'productId' =>
array (size=7)
0 => string '46' (length=2)
1 => string '46' (length=2)
2 => string '46' (length=2)
3 => string '46' (length=2)
4 => string '46' (length=2)
5 => string '46' (length=2)
6 => string '46' (length=2)
'save' => string '' (length=0)
uj5u.com熱心網友回復:
看起來問題是您回圈遍歷索引錯誤。
目前,您正在回圈通過 Product 但 $key 是使用錯誤的變數
foreach($_POST['Product'] as $key=>$check) {
'Product' =>
array (size=2)
0 => string '1' (length=1)
1 => string '4' (length=1)
因此,陣列中有兩個專案,索引為 0 和 1,所以你總是會像你的例子一樣抓住前兩行,你真的想要你設定的 $row["id"] 值或 $check變數在這里。
uj5u.com熱心網友回復:
考慮以下。
// Get the form
let form = document.querySelector('#porequest');
// Convert to a query string
let queryString = new URLSearchParams(new FormData(form)).toString()
console.log(decodeURI(queryString));
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X R7YkIZDRvuzKMRqM OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<!-- Assuming the following HTML Output -->
<form name="SalesAdd" action="sls-calc.php" method="post" enctype="multipart/form-data" id="porequest" />
<table>
<tr>
<td width="30px" style="text-align:center;">
<input type="text" name="item_price[]" id="item_price" value="5" />
<input type="text" name="item_rate[]" id="item_rate" value="8" />
<input type="text" name="item_discount[]" id="item_discount" value="5" />
<input type="checkbox" id="addProduct" name="Product[]" value="1" checked />
</td>
</tr>
<tr>
<td width="30px" style="text-align:center;">
<input type="text" name="item_price[]" id="item_price" value="7" />
<input type="text" name="item_rate[]" id="item_rate" value="9" />
<input type="text" name="item_discount[]" id="item_discount" value="4" />
<input type="checkbox" id="addProduct" name="Product[]" value="2" />
</td>
</tr>
<tr>
<td width="30px" style="text-align:center;">
<input type="text" name="item_price[]" id="item_price" value="3" />
<input type="text" name="item_rate[]" id="item_rate" value="7" />
<input type="text" name="item_discount[]" id="item_discount" value="3" />
<input type="checkbox" id="addProduct" name="Product[]" value="3" />
</td>
</tr>
<tr>
<td width="30px" style="text-align:center;">
<input type="text" name="item_price[]" id="item_price" value="2" />
<input type="text" name="item_rate[]" id="item_rate" value="4" />
<input type="text" name="item_discount[]" id="item_discount" value="2" />
<input type="checkbox" id="addProduct" name="Product[]" value="4" checked />
</td>
</tr>
</table>
<button type="submit" name="save" id="save" class="btn btn-info">
</form>
控制臺顯示了可能傳遞給 PHP 的內容。
item_price[]=5&item_rate[]=8&item_discount[]=5&Product[]=1&item_price[]=7&item_rate[]=9&item_discount[]=4&item_price[]=3&item_rate[]=7&item_discount[]=3&item_price[]=2&item_rate[]=4&item_discount[]=2&Product[]=4
這將在 PHP 中像這樣:
var_export($_POST)(
'Product' => array (
0 => '1',
1 => '4',
),
'item_price' => array (
0 => '5',
1 => '7',
2 => '3',
3 => '2',
),
'item_rate' => array (
0 => '8',
1 => '9',
2 => '7',
3 => '4',
),
'item_discount' => array (
0 => '5',
1 => '4',
2 => '3',
3 => '2',
),
)
您現在可以看到為什么如果您只使用 的索引$_POST['Product'],您將從其他陣列中獲得相同的兩個元素。
您需要在另一個欄位(例如隱藏欄位)中傳遞 ID,并查找選中的專案。
<td width="30px" style="text-align:center;">
<input type="text" name="item_price[]" id="item_price" value="'.$row["price"]. '"/>
<input type="text" name="item_rate[]" id="item_rate" value="'.$row["rate"]. '"/>
<input type="text" name="item_discount[]" id="item_discount" value="'.$row["Discount"]. '"/>
<input type="checkbox" name="productChk[]" />
<input type="hidden" name="productId[]" value="'.$row["id"]. '" />
</td>
現在你有一個標志可以檢查每一行。
foreach($_POST['productId'] as $key=>$val) {
if(isset($_POST["productChk"][$key])){
echo "<tr>";
echo "<td width='120px'>".$_POST["item_price"][$key]. "</td>";
echo "<td width='400px'>".$_POST["item_rate"][$key]. "</td>";
echo "<td width='90px'>".$_POST["item_discount"][$key]. "</td>";
echo "</tr>";
}
}
uj5u.com熱心網友回復:
重組表單資料對您有利,以便每個輸入的名稱直接參考它所屬的產品。考慮使用以下方案來構建您的表單:
<input type="text" name="item_price_'.$row["id"].'" id="item_price_'.$row["id"].'" value="'.$row["price"]. '"/>
<input type="text" name="item_rate_'.$row["id"].'" id="item_rate_'.$row["id"].'" value="'.$row["rate"]. '"/>
<input type="text" name="item_discount_'.$row["id"].'" id="item_discount_'.$row["id"].'" value="'.$row["Discount"]. '"/>
<input type="checkbox" id="addProduct_'.$row["id"].'" name="Product[]" value="'.$row["id"].'">
您現在看到每個鍵都將包含產品的 id,因此在處理表單時,您只需從中獲取選定的 id$_POST['Product']并選擇您需要的欄位,如下所示:
$products = [];
foreach($_POST['Product'] as $id){
$product[] = [
'id' => $id, // warning: should be sanitized
'price' => $_POST["item_price_$id"], // warning: should be sanitized
'rate' => $_POST["item_rate_$id"], // warning: should be sanitized
'Discount' => $_POST["item_discount_$id"], // warning: should be sanitized
];
}
var_dump($products);
die();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488917.html
