将以前的复选框发布到PHP
我有一个从如下查询创建的表:将以前的复选框发布到PHP,php,jquery,mysql,sql,Php,Jquery,Mysql,Sql,我有一个从如下查询创建的表: while ($row = mysqli_fetch_array($result)) { $fields[] = $row['unit']; $fields[] = $row['reservation']; $fields[] = $row['guest']; $fields[] = $row['arrival']; $fields[] = $row['departure']; $fields[] = $row[
while ($row = mysqli_fetch_array($result))
{
$fields[] = $row['unit']; $fields[] = $row['reservation']; $fields[] = $row['guest'];
$fields[] = $row['arrival']; $fields[] = $row['departure']; $fields[] = $row['balance'];
$fields[] = $row['checked_in'];
//rowid == id of table (the row corresponds to the row in the table)
$rowid = $row['reservation'];
$count = count($fields);
//make checkboxes
echo "<tr class='edit_tr' id='".$rowid."'>";
echo "<td><input type='checkbox' value='".$fields[1]."' name='checkins[]'/></td>";
for ($j = 0; $j < $count; $j++)
{
echo "<td><span id='".$fields[1]."'>", ($j+1 == $count ? ($fields[$j] == 0 ? "Not Checked In" : "Checked In") : "".$fields[$j]."");
echo "</span></td>";
}
echo "</tr>";
//empty array
$fields = "";
}
我知道我不能发布未检查的框,但我需要一种方法来告诉PHP文件什么是未检查的。以下是我到目前为止的想法:
1查询数据库中的所有签入=1,其中签出=0我只在表中显示签出=0的行,然后如果id不在POST结果中,则设置签入=0
2将一个ajax数组发送到另一个PHP文件,其中包含以前选中但现在未选中的所有框的列表
我相信还有更多,但我就是这么想的。不过,它们似乎都是权宜之计。有人有更好的解决方案吗?您的第一个选项实际上是最好的选择,但我也看到人们在每个复选框元素后面都有一个同名的隐藏元素,值为0。这将导致你总是得到一个值张贴。选中的复选框将覆盖隐藏的元素。如果将id作为名称的一部分,也可能会有所帮助。name=checkins[$id]将id包含在名称中有什么好处?它允许您使用值1表示通过复选框选中,0表示隐藏元素,以确定是否选中了特定的$id。foreach$\u POST['checkins']作为$id=>$value
if(isset($_POST['checkins']))
{
//open connection
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//create WHERE clause for new checkins
$where = " WHERE id =";
foreach ($_POST['checkins'] as $id) {
$where .= " $id OR id =";
}
//remove last 8 characters from WHERE clause
$where = substr($where, 0, -8);
echo $query = "UPDATE reservations
SET checked_in = CASE
WHEN checked_in = 0 THEN 1 END
$where)";
mysqli_query($con, $query);
mysqli_close($con);
}