如何通过提交表单传递多个复选框值?

时间:2015-09-29 08:53:43

标签: php database

    <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>

multiedit.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被传递到数组中(数组([checkbox2] =&gt;数组([0] =&gt; 378 [1] =&gt; 379)))。但它显示了用户的每条记录。

更新时间:下午3:58 30/09/2015 SGT已解决。这里和那里存在误解。

2 个答案:

答案 0 :(得分:0)

只发送名称和值,因此一个选项是在PHP中调用名称。 另一种选择是研究Jquery。

另外,您应该将输入放在表单中并将文本包装在span:

<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>

所以现在当你向你的php添加print_r($_POST);时,你会看到如何调用它。

答案 1 :(得分:-1)

对于pass multiple复选框值,使用输入名称作为数组。

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