【问题标题】:Is there a way of updating a large form with checkboxes effectively?有没有办法有效地更新带有复选框的大型表单?
【发布时间】:2014-08-22 14:59:55
【问题描述】:

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

仅作说明:

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

有什么方法可以将 changed 复选框添加到 save_dbarray 中? (只有选中的框会发布到 $_POST,但如果它们已更改,我希望未选中的值也成为更新的一部分)我必须对每个发布的值进行一些昂贵的检查,因此

更新 我不想遍历所有 1000 个复选框。我只想遍历更改的(从选中到未选中或从未选中到选中)复选框,但在上述情况下 $posted_values 只会返回具有选中值的复选框(从未选中到选中)

<?php
//I DONT want to have to do like this:
for {$i=0;$i<1000;$i++) {
    $prodnr = 'product-' . $i;
    $chkbox = $_POST[$prodnr];
    $update = 0;
    if ($chkbox == 'on') {
        $update = 1;
    }
   //Do some "expensive" checking for every value

   $save_dbarray[$key] = $update;
}

//Do the actual updating to databased based on array `save_dbarray`

【问题讨论】:

  • 你能更清楚地解释你想要的结果吗?
  • 你可以试试javascript/jquery,比如document.getElementById('product').checked。也将您的复选框名称作为数组 (name='product[]') 可能更容易
  • @arunrc - javascript/jquery 与我的问题无关。是的,当然我可以使用像 product[] 这样的数组,但我的问题只是为了说明我遇到的问题
  • 只是一个想法。将当前检查的详细信息保存在一个数组中。然后在更新后与数组进行比较以获取更新的复选框。但问题是您想遍历所有复选框以查找更新。

标签: php checkbox


【解决方案1】:

您可以使用HTML array inputs and PHP 来做同样的事情。

示例代码如下所示。

<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);
?>

编辑

您需要遍历$_POST['products'] 才能找到新的选中项,您需要遍历$already_selected 才能找到未选中的项。

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

【讨论】:

  • 如果我将已选中的产品更改为未选中(从已选中)该产品将不会包含在 $_POSTS['products'] 中?
  • 是的,最好的方法是在此之前放置一个选择查询以查找已选择的所有内容。如果您确实需要它通过表单传递,请将其放在表单中的隐藏字段中,并在下次提交时传递。
  • 或者,我用服务器端的方式更新答案,但是如果您需要在表单中显示已经选中的选项,您需要再次选择两次。
猜你喜欢
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
相关资源
最近更新 更多