【问题标题】:Sending checked checkbox ids to the database using jquery [duplicate]使用jquery将选中的复选框ID发送到数据库[重复]
【发布时间】:2017-07-30 09:57:21
【问题描述】:

我有一个清单,用户需要检查多个复选框,然后我应该计算每个复选框的总价格并显示它。问题是我应该将选中的复选框插入数据库。因此,当我使用 isset() 函数时,我遇到了错误。所以我想改用 jquery 并将检查的 id 发送到数据库,但我不知道该怎么做。

    <form method="post" action="#">
            <div class="agileinfo_services_grids">
                <?php 
                    $sql = "SELECT * FROM services";
                    $result = mysqli_query($connection, $sql);
                    $num_rows = mysqli_num_rows($result);

                    if($num_rows > 0) {
                        while ($row = mysqli_fetch_assoc($result)) {

                            $price = $row['price'];
                            $id = $row['id'];
                            echo "<div class='col-md-4 agileinfo_services_grid'>
                                    <div class='agileinfo_services_grid1'>
                                        <div class='price'><h3>$" . $row['price'] . "</h3></div>
                                        <h4>" . $row['title'] . "</h4>
                                        <p>" . $row['details'] . "</p>
                                        <div class='agileinfo_services_grid1_pos'>
                                            <input id='$id' name:'checkbox[]' class='icon-checkbox my-activity' type='checkbox' value='$price'/>
                                            <label for='$id'>
                                                <span class='glyphicon glyphicon-unchecked unchecked'></span>
                                                <span class='glyphicon glyphicon-check checked'></span>
                                            </label>
                                        </div>
                                    </div>
                                </div>";            
                        } 
                    } else {
                        echo "No Current Services Available";
                    }
                ?>
            </div>
            <div class="total">
            <div class="total-left">
                <p>Total :<span>$<input type="text" id="amount" readonly></span>
            </div>
            <div class="total-right">
                <input type="submit" value="Check Out" name="submit">
            </div>
            <div class="clear"> </div>
        </div>
        </form>

下面是我用来计算复选框总价的函数。如何修改它以将复选框的 id 也发送到数据库。

<script type="text/javascript">
    $(document).ready(function() {        
        $(".my-activity").click(function(event) {
            var total = 0;
            $(".my-activity:checked").each(function() {
                total += parseInt($(this).val());
                $.post("services.php", {id: this.id, checked:this.checked});
            });

            if (total == 0) {
                $('#amount').val('');
            } else {                
                $('#amount').val(total);
            }
        });
    });    
</script>

【问题讨论】:

    标签: javascript php jquery checkbox


    【解决方案1】:

    按照此步骤将选定的 ID 发送到数据库。

    HTML:

    <div id="checkboxes">
        <input id="chkbx_0" type="checkbox" name="c_n_0" checked="checked" />Option 1
        <input id="chkbx_1" type="checkbox" name="c_n_1" />Option 2
        <input id="chkbx_2" type="checkbox" name="c_n_2" />Option 3
        <input id="chkbx_3" type="checkbox" name="c_n_3" checked="checked" />Option 4
    </div>
    

    使用这个 jquery 脚本:

    var selected_ids = [];
    $('#checkboxes input:checked').each(function() {
        selected_ids.push($(this).attr('id'));
    });
    
    /* to sending ids to database using ajax */
    $.ajax({  
        type: 'POST',  
        url: 'test.php', 
        data: { selected_ids: selected_ids },
        success: function(response) {
            alert(response);
        }
    });
    

    【讨论】:

    • 感谢您的回复,但问题是如何在数据库中插入 id。我只通过键入 var_dumb($_POST) 设法在 test.php 中看到了数组
    • 您要在其中插入 id 的表结构是什么......
    • 我有一个名为 orders 的表,我应该在其中插入 user_id 和提交订单时的时间戳。其次,我有一个 serviceOrder 表,其中包含 order_id 和 service_id(实际上是复选框)。再次感谢您的快速回复
    • 所以我必须使用服务表动态创建复选框,如我原来的问题所示,然后计算总价并立即打印我已经实现的。现在的问题是我想在表 orderServices 中插入我应该从你提供给我的 ajax 函数中获得的 order_id 和 service_id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2011-11-17
    • 2013-11-09
    • 1970-01-01
    • 2018-12-15
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多