如何通过提交表单传递多个复选框值? [英] 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: <input type="text" name="search" />
<input type="submit" name="submit" value="Search">
</form></td>
<td><form method="post" action="grouprank.php">
Rank: <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
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)))。但是它显示了用户的每个记录。
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.
更新从3:58 PM 30/09/2015 SGT它解决了。
Update as of 3:58 PM 30/09/2015 SGT it is solved. Misconceptions here and there.
推荐答案
对于通过多个复选框值,使用输入名称作为数组。
For pass multiple checkbox value use input name as array.
<input type="checkbox" name="checkbox[]" />
这篇关于如何通过提交表单传递多个复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!