[英]Radio Buttons within a PHP while loop
我正在以HTML和PHP的表格形式顯示來自MySQL DB的數據。 在這里,每個“訂單”都有自己的“狀態”,即每行應有2個單選按鈕以更改其狀態。 盡管它顯示了所有單選按鈕,但問題是我無法為每行選擇一個單選按鈕,我只能為所有行選擇一個單選按鈕!
<?php
$con3 = mysqli_connect($mysql_hostname, $mysql_usrnm,$mysql_pass, "customer") or die("Ouch!");
$sql_query = " SELECT * FROM order_info WHERE deptt='".$_SESSION['deptt']."'";
$result = mysqli_query($con3, $sql_query);
if (!$result) {
printf("Error: %s\n", mysqli_error($con3));
exit(); }
echo '<table border="0px">';
echo'<th>Product</th><th>Type</th><th>Quantity</th><th>Order Status</th>';
while ($row = mysqli_fetch_array($result)){ //Tabular Data
$type=$row["type"];
$make=$row["make"];
$quantity=$row["quantity"];
echo "<tr>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo '<td><cb><input type="radio" name="status" value="not approved">Not Approved<br>
<input type="radio" name="status" value="approved">Approved</cb></td>';
}
echo "</table>";
?>
我該如何解決?
您應該更改輸入的name
。 我猜您還有一個名為id
的數據庫字段,因此我將在示例中使用它:
echo '<td><cb><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved">Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</cb></td>';
同樣,在處理表單時,可以通過對id = 2的行使用例如$_POST['status'][2]
來獲取值。 您可以輕松地循環$_POST['status']
。
在“名稱”值的末尾添加一個數字,我建議“訂單”的內部ID
name="status123"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.