duanqiongniu1469 2015-11-12 19:00 采纳率: 100%
浏览 111
已采纳

如何将复选框附加到提交

I have unknown amount of checkbox inputs on a page created based on rows in an sql database.

The checkboxes look like:

<input type="checkbox" class="deletefunc" value="<? echo $mid; ?>">

The checkboxes are intended, when checked and submitted, to delete a row from the database based on the value of $mid

The checkboxes could be either loose (not contained in any forms), or each could be contained in its own form (without a submit button). There is no way however to contain all the checkboxes in one form.

The reason is it's displayed in a loop with output like this:

<tr>
<td><input type="checkbox" class="deletefunc" value="<? echo $mid; ?>"></td>
<td><input type="checkbox" class="star" value="1"> <!-- jquery/ajax onclick function --></td>
<td><a href></td>
<td><form name....><input type="submit" value="something"></form></td>
<tr>

So what I need help understanding is, how to gather all the checkboxes based on classname, and attach them to a submit button....

And further, how would I write the loop function so it knows how many deletes to perform? What I mean is, how would I write a loop that would get the values from an unknown amount of inputs. Normally I know how many inputs there are and what the names are so its a matter of assigning a variable for each input.... I've never done this with an unknown amount of inputs.

  • 写回答

1条回答 默认 最新

  • dpw63348 2015-11-12 19:14
    关注

    I would do it like this:

    HTML:

    <input type="checkbox" class="deletefunc" value="<? echo $mid; ?>">
    <button id="delete-them">Delete Them</button>
    

    JS:

    $(function() {
        $('#delete-them').on('click', function() {
            var data = $('.deletefunc').attr('name', 'delete[]').serialize();
            $.ajax('delete-them.php', { 
                method: 'POST',
                data: data, 
                success: function() {
                    alert('deleted!');
                }
             });
         });
    });
    

    PHP:

    <?php
        $ids = $_POST['delete'];
        $params = array_fill(0, count($ids), "?");
        $sql = "DELETE FROM some_table WHERE id IN (" . implode(",", $params) . ")";
        //DELETE FROM some_table WHERE id IN(?,?,?,?);
        $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass');
        $stmt = $pdo->prepare($sql);
        $stmt->execute($ids);
    ?>
    

    So we basically listen for a click on our button, when it happens, we set the name (with [] at the end so it becomes an array) for all of the items with the class we care about, so that when we serialize those items we know how to reference it in our PHP.

    We then POST that data over to the server, where we read the array we just sent, see how many there are, and then create a query to delete those items.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办