无法从复选框中获取值并在另一个php文件上使用

时间:2015-09-29 04:48:38

标签: php mysql

我创建了一个复选框,允许用户同时进行多次编辑,但我无法弄清楚如何将提交按钮链接到选中的复选框,然后在下一页显示所选的选项。

这是复选框页面

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

        <td>&nbsp;

        <td>&nbsp;
<input type="checkbox" id ='checkbox1' name="checkbox1" onclick="load();" value="Bike">Include previous service terms</td>
    </div>
    <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\" id ='checkbox2' value=\"checkbox2\" >" . "</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>

这是它应该导致的页面

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

<div id="container4"><?php


require ("dbfunction.php");
$con = getDbConnect();
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>" . $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>";
} else {

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

                </tr>

3 个答案:

答案 0 :(得分:0)

错误

  1. 错误</div关闭(缺少></div> <div id="show">
  2. 标记错误声明<p><table>
  3. 此处<td>无法启动<input type="submit" value="Submit" ></td>
  4. >
  5. 上遗漏<input type="submit" value="Submit" ></td
  6. 在此<tr>
  7. 上没有开始<input type="submit" value="Submit" ></td> </tr>
  8. 在表格中应该只有一组<th>代码。但是在你的代码中有两套。
  9. 等...

答案 1 :(得分:0)

echo "<tr><th>" . "<input type = \"checkbox\" id ='checkbox2' value=\"checkbox2\" >" . "</th>";

在此行中,您尚未指定名称属性。它应该看起来像

echo "<tr><th>" . "<input type = \"checkbox\"  name ='checkbox2[]'  id ='checkbox2' value=\"checkbox2\" >" . "</th>";

因为它有多个值,这就是我们在名称中使用[]数组参数的原因。

你可以从下一页获取它 假设方法是post-$ _POST ['checkbox2'],它将返回选中的复选框数组。

答案 2 :(得分:0)

除了HTML错误,您需要使用表单标记将您发送到page2.php。我无法检查你的其他工作,这有点混乱。

<?php
require ("dbfunction.php");
$con = getDbConnect();
?>
<form name="whatever" id="whatever" action="page2.php" method="post">
    <table>
        <tr>
            <td colspan="6">&nbsp;</td>
            <td>
                <input type="checkbox" id ='checkbox1' name="checkbox1" onclick="load();" value="Bike">
                Include previous service terms
                <div id="show"></div>
            </td>
        </tr>
        <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>
        <?php
        if (!mysqli_connect_errno($con)) {
                $queryStr = "SELECT * FROM crewlist";
                $result = mysqli_query($con, $queryStr);
                $i = 0;
                while ($row = mysqli_fetch_array($result)) {
                        if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date']) {
?>
                            <tr>
                                <td>
                                    <input type = "checkbox" id ="checkbox<?php echo $i; ?>" name="checkbox[]" value="checkbox<?php echo $i; ?>" />
                                </td>
                                <td>
                                    <a href="viewcrew.php?id=<?php echo $row['crew_id']; ?>"><?php echo $row["crew_name"]; ?></a>
                                </td>
                                <td>
                                    <?php echo $row["crew_rank"]; ?>
                                </td>
                                <td>
                                    <?php echo $row["start_date"]; ?>
                                </td>
                                <td>
                                    <?php echo $row["end_date"]; ?>
                                </td>
                                <td>
                                    <?php echo $row["watchkeeping"]; ?>
                                </td>
                                <td>
                                    <?php echo $row["active"]; ?>
                                </td>
                            </tr>
                            <?php
                            $i++;
                        }
                }
        }
        ?>
        <tr>
            <td colspan="7">
                <input type="submit" name="submit" value="Submit" >
            </td>
        </tr>
    </table>
</form>

<强>使page2.php

您可以在此处通过$_POST访问表单值来获取表单值。

<?php
include('header.php');
require("dbfunction.php");
$con = getDbConnect();
print_r($_POST);
?>
<div id="container4">
    <table>
<?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'])) {
?>
        <tr>
            <th colspan="2"><?php echo $row["crew_name"]; ?>:</th>
        </tr>
        <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>
<?php                   }
            }
?>
        <tr>
            <td><input type="submit" value="Submit" ></td>
        </tr>
<?php }
?>
    </table>
</div>