doumo0206 2017-07-30 09:57
浏览 12

使用jquery [duplicate]将选中的复选框ID发送到数据库

This question already has an answer here:

I have a checklist that users need to check more than one checkbox and then I should calculate the total price of each checkbox and display it. The problem is I should insert the checked checkbox into the database. So when I am using isset() function I am having an error. So I am thinking to use jquery instead and sending the ids checked to the database but I don't know how to do it.

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

below is the function that I used to calculate the total price of the checked boxes. How can I modify it to send to the database the ids of the checked boxes as well.

<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>
</div>
  • 写回答

1条回答 默认 最新

  • dongxu4023 2017-07-30 10:06
    关注

    Follow this steps to send selected ids to database.

    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>
    

    Use this jquery script:

    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);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)