簡體   English   中英

發布選定復選框的下拉值

[英]Post drop down value of selected checkboxes

我有一個在 PHP 中動態創建的 html 表。該表有復選框、名稱價格和數量。數量是下拉列表。我想知道如何根據選定的復選框發布所有這些值。這是小 snipet。我想要的是用戶將選中復選框,我想將名稱、價格和所選數量發布到另一個頁面 cart.php。

我現在正在工作的是我只能通過執行 $_POST["checkboxes"] 來發布選定的復選框值。但我不知道為那些選定的復選框發布值。請幫助..我正在嘗試學習 PHP。

<form action="cart.php" name="myform" id="menuform" method="post" >
echo "<label>"."Appetizers"."</label>";
echo "<center>";
echo "<table class='appetizerstable'>";
echo "<thead>";
echo "<tr>";
echo "<th>";
echo "Select";
echo "</th>";
echo "<th>";
echo "Name";
echo "</th>";
echo "<th>";
echo "Price";
echo "</th>";
echo "<th>";
echo "quantity";
echo "</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

while($row = mysqli_fetch_array($appetizers)) {
echo "<tr>";
echo "<td>" ."<input type='checkbox' name ='checkboxes[]'  value=".$row['id'].">".
             "</td>";
echo "<td>" ."<label name='foodname[]'>". $row['name']."</label>" . "</td>";
echo "<td>" ."<label name='foodprice[]'>". $row['price']."</label>" . "</td>";
echo "<td>"."<select id='quantity[] name='quantity[]'>".
"<option value='1'>1</option>".
"<option value='1'>2</option>".
"</select>".
"</td>";
echo "</tr>";

}
echo "</tbody>";
echo "</table>";
echo "<center>";

您不需要發送名稱和價格值,因為您可以在發送表單數據時通過知道該項目的 id 來從數據庫中檢索這些值。 因此,我們將只關注所檢查物品的quantitiesid

您正在嘗試做的主要問題是,發送表單時, quantity[]表單數據和checkboxes[]表單數據在數組中不會有相同數量的項目......除非您勾選每個項目. 發生的情況是checkboxes僅在復選框被選中時發送數據,而quantity[]在選擇選項時獲取數據發送,默認情況下始終選擇一個。 所以基本上每個數量選擇數據都會被發送,無論是否檢查。 因此很難確定哪些數量數組項屬於復選框數組項。 但是有一個解決方案......一個復雜的解決方案,但仍然是一個只有 php 的解決方案。

這是新的 HTML,為了便於閱讀,我對其進行了一些清理

<form action="cart.php" name="myform" id="menuform" method="post" >
    <label>Appetizers</label>
    <center>
        <table class="appetizerstable">
        <thead>
            <tr>
                <th>Select</th>
                <th>Name</th>
                <th>Price</th>
                <th>quantity</th>
            </tr>
        </thead>
        <tbody>

            <?php while($row = mysqli_fetch_array($appetizers)) : ?>
                <tr>
                    <td>
                        <input type="checkbox" name ="checkboxes[]"  value="<?php echo $row['id'] ?>">
                    </td>
                    <td>
                        <label><?php echo $row['name']; ?></label>
                    </td>
                    <td>
                        <label><?php echo $row['price']; ?></label>
                    </td>
                    <td>
                        <select id="quantity[]" name="quantity[]">
                            <option value="<?php echo '1-' . $row['id']; ?>">1</option>
                            <option value="<?php echo '2-' . $row['id']; ?>">2</option>
                        </select>
                    </td>
                </tr>

            <?php endwhile; ?>
        </tbody>
        </table>
    <center>
</form>

好的,那么發生了什么變化……好吧,看看新的quantity[]值。 我附加了一個破折號,后跟行 ID。 現在每個值都有一個唯一值,我們有一種方法可以將數量值與復選框值相關聯。 接下來我們將在cart.php 文件中解決這個問題。 以下是如何做到這一點:

購物車.php

<?php 

if(isset($_POST['checkboxes']) && isset($_POST['quantity']) ) {
    $checkboxes = $_POST['checkboxes'];
    $quantities_unfiltered = $_POST['quantity'];

    $quantities = array();

    foreach($quantities_unfiltered as $unfiltered) {

        $filtered = explode('-', $unfiltered);
        // $filtered now equals a 2 item array
        // $flitered[0] = the quantity value
        // $filtered[1] = the id

        // create an associative array for easy access to the quantity by using the id as the array key

        $quantities[ $filtered[1] ] = $filtered[0];

    }

    // now we can figure out the quantity values for each checkbox checked
    // test it out by echoing the values

    foreach($checkboxes as $id) {
        echo 'Item with id of ' . $id . ' was selected with a quantity of ' . $quantities[$id] . '<br>';
    }
}
?>

希望對你有所幫助。

更新

我在cart.php 示例中添加了一個if 語句,並從name 屬性中刪除了foodprice[]foodname[] ,因為它們無論如何都不會作為POST 數據發送,因為html 不是表單元素。 我對此有點挑剔。

您可以在更改下拉值時使用 jquery 提交表單

<select name="" id="" onchange="this.form.submit()"> .......... ......... </select>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM