从数据库中检索值,选择或取消选中JQuery复选框

我的jQuery中存在一个问题。 我想使用AJAX从数据库中检索数据。如何选择并将这些值传递给php文件并获取多个值。

例如 – 当我选中复选框时 ,ajax将返回所选复选框的值 。 如果我取消选中相同的复选框,则该值将被删除

这里的复选框是:

checkboxes.php

这里是jQuery:

 $(document).ready(function() { var check=[]; $('input[type=checkbox]').on('change', function() { var check=$(this).val(); console.log($(this).val()); $.ajax({ type:'POST', data:{ "extra" : check}, dataType : "JSON", url : "login.php", success:function(response){ if(response){ totalCost=10+response; $('#cost').text(totalCost); } } }); }); }); 

这里的PHP代码 –

 if (isset($_POST['extra'])) { $query=mysqli_query($con,"select price from extras where name_of_extra ='$_POST[extra]'"); while ($row = mysqli_fetch_array($query)) { echo json_encode($row['price'],JSON_NUMERIC_CHECK); } } 

数据库图像 –

数据库的形象

我想检查倍数并通过ajax接收多个值,当我取消选中复选框时,该值将从跨度总计中删除 。 如果有任何错误,那么我很抱歉。 我是一个更大的人。 我希望你们都能支持我,提前做好thnku。

HTML

  0 

JS

 $(document).ready(function () { var check = []; $('input[type=checkbox]').on('change', function () { var checked = 0; // assigning ZERO for unchecked if ($(this).prop('checked') == true) { // checking checkbox status checked = 1; // ONE for checked } var totalCost = $("#totalCost").text(); // getting previous totalcost var check = $(this).val(); // two parameters // data json object // callback function getData({"extra": check}, function (response) { var content = ''; if (response) { //response is json object if (checked) { // if checkbox is checked content = '
' + response['price'] + '
'; //create another variable which have div with price and id totalCost = parseInt(totalCost) + response['price']; // add new price in previous price } else { $('#' + response['id']).remove(); // remove price div targeting with id totalCost = parseInt(totalCost) - response['price']; // subtract price from previous price } $("#cost").prepend(content); // prepend new content $("#totalCost").html(totalCost); } }); }); function getData(data, callback) { $.ajax({ type: 'POST', data: data, dataType: "JSON", url: "login.php", success: callback }); } });

PHP代码的变化

 if (isset($_POST['extra'])) { $query=mysqli_query($con,"select * from extras where name_of_extra ='$_POST[extra]'"); while ($row = mysqli_fetch_array($query)) { echo json_encode($row,JSON_NUMERIC_CHECK); } } 
Interesting Posts