Php 是否有一种方法可以有效地更新带有复选框的大型表单?

Php 是否有一种方法可以有效地更新带有复选框的大型表单?,php,checkbox,Php,Checkbox,在将带有复选框的大型表单有效更新到数据库时遇到问题 仅作说明: <form action="save.php" method="post"> <?php for {$i=0;$i<1000;$i++) { echo '<input type="checkbox" name="product-' . $i . '">'; } <input type="submit"> </form> <?php $posted_values =

在将带有复选框的大型表单有效更新到数据库时遇到问题

仅作说明:

<form action="save.php" method="post">
<?php
for {$i=0;$i<1000;$i++) {
echo '<input type="checkbox" name="product-' . $i . '">';
}
<input type="submit">
</form>


<?php
$posted_values = $_POST;
foreach($posted_values as $key=>$p) {
    $chkbox = $posted_values[$p];
    $update = 0;
    if ($chkbox == 'on') {
        $update = 1;
    }
   //Do some "expensive" checking for each posted value

   $save_dbarray[$key] = $update;
}

//Do the actual updating to databased based on array `save_dbarray`
你可以用它来做同样的事情

示例代码如下所示

<form action="save.php" method="post">
<?php
for ($i=0;$i<1000;$i++) {
    echo '<input type="checkbox" name="products[]" value="' . $i . '">&nbsp;'. $i .'<br>';
}
?>
<input type="submit">
</form>


<?php
print_r($_POST['products']); // Will contain your desired output
foreach($_POST['products'] as $i) {
    $save_dbarray[$i] = 'on'; // 'on' or whatever value if you need.

    // Actually you just need $_POST['products'], no need for this loop.
}

print_r($save_dbarray);
?>

你能更清楚地解释你想要的结果吗?你能试试javascript/jquery吗,比如,
document.getElementById('product')。选中
。另外,将复选框名称指定为数组(name='product[])可能更方便easy@arunrc-javascript/jquery与我的问题无关。是的,我当然可以使用像product[]这样的数组,但我的问题只是为了说明我的想法。将当前选中的详细信息保留在数组中。然后在更新后与数组进行比较,以获得更新的复选框。但问题是,您希望通过循环所有复选框来查找更新。如果我将选中的产品更改为未选中(从选中),则该产品将不会包含在$\u POSTS['products']中?是的,最好的方法是在此之前放置一个select查询,以查找已选中的所有产品。如果您确实需要通过表单传递答案,请将其放入表单中的隐藏字段中,并在下次提交时传递。或者,我使用服务器端的方式更新答案,但如果您需要在表单中显示已选中的答案,您需要再次选择两次。
<form action="save.php" method="post">
<?php
for ($i=0;$i<1000;$i++) {
    echo '<input type="checkbox" name="products[]" value="' . $i . '">&nbsp;'. $i .'<br>';
}
?>
<input type="submit">
</form>


<?php
print_r($_POST['products']); // Will contain your desired output
foreach($_POST['products'] as $i) {
    $save_dbarray[$i] = 'on'; // 'on' or whatever value if you need.

    // Actually you just need $_POST['products'], no need for this loop.
}

print_r($save_dbarray);
?>
    <?php

    // Select from db or something
    $already_selected = array(2,3);

    foreach($_POST['products'] as $i) {
        if(!in_array($i,$already_selected)){
            $save_dbarray[$i] = 'checked_update';
        }
    }
    foreach($already_selected as $j) {
        if(!in_array($j,$_POST['products'])){
            $save_dbarray[$j] = 'unchecked_update';
        }
    }
    print_r($save_dbarray);

    // Do db update and select again and update $already_selected to display the checked ones
?>

<form action="save.php" method="post">
<?php
    for ($i=1;$i<10;$i++) {
        $checked = in_array($i, $already_selected) ? 'checked' : '';

        echo '<input type="checkbox" name="products[]" value="' . $i . '" ' . $checked . '>&nbsp;'. $i .'<br>';
    }
?>
<input type="submit">
</form>