簡體   English   中英

如何通過提交表單傳遞多個復選框值?

[英]How to pass multiple checkbox values by submitting form?

    <p><?php include 'header.php'; ?></p>
    <div align="justify">


        <td><form method="post" action="search.php">
                Name:&nbsp;<input type="text" name="search" />
                <input type="submit" name="submit" value="Search">
            </form></td>


        <td><form method="post" action="grouprank.php">
                Rank:&nbsp;<input type="text" name="groupby" />
                <input type="submit" name="submit" value="Group by">
            </form></td>

        <?php
        require ("dbfunction.php");
        $con = getDbConnect();
        ?>






    //start here
<form name="form" id="form" action="multiedit.php" method="post">

    <div id="show">        
    </div>
    <p><table>
        <tr>
            <th>Tick</th>
            <th>Name</th>
            <th>Rank</th>
            <th>Start Date</th>
            <th>End Date</th>
            <th>Watchkeeping</th>
            <th>Active</th>
        </tr> <!-- database -->
        <tr>
            <?php
            if (!mysqli_connect_errno($con)) {

                $queryStr = "SELECT * " .
                        "FROM crewlist";
            }
            $result = mysqli_query($con, $queryStr);
            while ($row = mysqli_fetch_array($result)) {
                if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date']) {

                    echo "<tr><th>" . "<input type = 'checkbox' name = 'checkbox2[]' value='" . $row['crew_id']. "' >" . "</th>";
                    echo "<th>" . "<a href=\"viewcrew.php?id=" . $row['crew_id'] . "\">" . $row["crew_name"] . "</a>";
                    echo "<th>" . $row["crew_rank"] . "</th>";
                    echo "<th>" . $row["start_date"] . "</th>";
                    echo "<th>" . $row["end_date"] . "</th>";
                    echo "<th>" . $row["watchkeeping"] . "</th>";
                    echo "<th>" . $row["active"] . "</th>";
                } else {

                }
            }
            ?>

        </tr>
        <input type="submit" value="Submit" ></td>
        </tr>

    </table>
    </form>
</body>

多編輯.php

<?php include 'header.php'; ?>

<div id="container4"><?php


require ("dbfunction.php");
$con = getDbConnect();

$checkbox2 = $_POST['checkbox2'];

if (!mysqli_connect_errno($con)) {
$str =implode($checkbox2);

$queryStr = "SELECT * " .
        "FROM crewlist WHERE  ($str)";
}
$result = mysqli_query($con, $queryStr);
//if (!$check1_res) {
//    printf("Error: %s\n", mysqli_error($con));
//    exit();
//}
print_r($_POST);
while ($row = mysqli_fetch_array($result)) {
if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date'])     {

    echo "<tr><th>" . $row["crew_name"] . ":</th><br>";
    echo "                    <tr>
                    <td>Shift 1:</td>
                    <td><input type=\"time\" name=\"start_hour\" value=\"start_hour\" id=\"start_hour\" step=\"1800\" required> to <input type=\"time\" name=\"end_hour\" value=\"end_hour\" id=\"end_hour\" step=\"1800\" required>
                    </td>       
                </tr>
                <tr>
                    <td>Shift 2:</td>
                    <td><input type=\"time\" name=\"start_hour2\" value=\"start_hour2\" id=\"start_hour2\" step=\"1800\" required> to <input type=\"time\" name=\"end_hour2\" value=\"end_hour2\" id=\"end_hour2\" step=\"1800\" required>
                    </td>       
                </tr><br><br>";
}
} 
?>

這是流程應該如何工作,我檢查了幾條記錄,復選框應該將用戶的 id 傳遞到編輯頁面,在那里它應該顯示被檢查的記錄。 使用 print_r($_POST) 顯示 id 被傳遞到數組中,就像這樣 (Array ( [checkbox2] => Array ( [0] => 378 [1] => 379 ) ) ) ) 。 但是它顯示了用戶的每條記錄。

更新截至 30/09/2015 SGT 下午 3:58 已解決。 這里和那里的誤解。

只發送名稱和值,因此一種選擇是在 PHP 中調用名稱。 另一種選擇是研究 Jquery。

另一件事,您應該將輸入放在表單中並將文本包裹在一個跨度中:

<form name="form" id="form" action="multiedit.php" method="post">
    <input type="checkbox" id='checkbox1' name="checkbox1" onclick="load();" value="Bike">
    <span>Include previous service terms</span>
</form>

所以現在當你添加print_r($_POST); 到您的 php,您將看到如何調用它。

對於傳遞多個復選框值,使用輸入名稱作為數組。

<input type="checkbox" name="checkbox[]" />

暫無
暫無

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

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