【问题标题】:How to pass multiple checkbox values by submitting form?如何通过提交表单传递多个复选框值?
【发布时间】:2015-09-29 08:53:42
【问题描述】:
    <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 像这样传递到数组中 (Array ( [checkbox2] => Array ( [0] => 378 [1] => 379 ) ) ) 。但是它显示了用户的每条记录。

更新至 2015 年 9 月 30 日下午 3:58 SGT 已解决。到处都有误解。

【问题讨论】:

  • “链接中没有id”什么链接?
  • 通常如果要提交表单,应将其链接到用户的ID。例如,multiedit.php?id=363
  • 我不确定我是否遵循,您的表单是通过 $_POST 而不是 $_GET 提交的,并且我可以看到您的复选框包含静态文本值,而不是 ID
  • 是的,这就是我打算前进的方向,但我似乎无法获得正确的代码。
  • .... 什么?你们都准备好在别处分配动态值了,我真的不明白你的问题。

标签: php database


【解决方案1】:

只发送名称和值,因此一种选择是在 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 中时,您将看到如何调用它。

【讨论】:

    【解决方案2】:

    要传递多个复选框值,请使用输入名称作为数组。

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

    【讨论】:

    • 好的,如果我使用 if (!mysqli_connect_errno($con)) { $queryStr = "SELECT * " 就会出错。 “来自船员名单 WHERE 船员 ID =”。 $复选框2。 "";
    • 数组转字符串(注意)
    • 对于这第一次内爆,您使用“$str =implode($checkbox2);”将数组以逗号分隔的值排列功能。然后在选择查询中使用“WHERE IN ($str)”
    • 我猜这适用于数组到字符串的转换错误。我还有最后一个常见错误,mysqli_fetch_array() 期望参数 1 是 mysqli_result,给定的布尔值。直到今天我仍然不明白这个错误。
    猜你喜欢
    • 2012-10-28
    • 2010-12-21
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    相关资源
    最近更新 更多