Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

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

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
", 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>";
}
} 
?>

This is how the flow should work, I check several records, the checkbox should pass the id of the user to the edit page where it should display the records that are checked. Using print_r($_POST) there shows the id being passed into the array like so (Array ( [checkbox2] => Array ( [0] => 378 [1] => 379 ) ) ) . However it displayed out every record of the users.

Update as of 3:58 PM 30/09/2015 SGT it is solved. Misconceptions here and there.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Only the name and value is send, so one option is calling the name in your PHP. Another option would be to look into Jquery.

Another thing, you should put the input inside your form and wrap your text in a 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>

So now when you add print_r($_POST); to your php you'll see how to call it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...