javascript - jQuery ajax call, pass data: from looping through elements -


is there way loop through elements , pass element value in data: {} option of $.ajax()?

eg.

$res = pg_query($con, "select * tbl_salary_deductions");            while($row = pg_fetch_assoc($res)) {         echo "<input type=checkbox class='dlccat' id='" .  $row['dlcid']. "'> category " . $row['dlccategory'] . "<br>"; } 

creates checkboxes. in call pass checked ones can process them server side:

$.ajax({         url: 'ajax/register_driver.php',         data: {             //dlccat             $.each($(".dlccat:checked"), function() {                 dlcat + $(this).id: true,             });         },         success: function () {          },         error: function () {             alert('data update failed. please try again!');         }     }) 

enter image description here

need save in db values of checked items per pic.

$.ajax() implementation of xmlhttprequest exposed jquery. consider function call passing initialisation parameters url, data, datatype etc. "data" option pass $.ajax should object. wouldn't evaluated/run function expecting in case.

the above code give syntax error because trying evaluate loop (the each statement) inside object.

the correct way first form object using each statement , pass object $.ajax method.

would this.

var obj = {}; $(".dlccat:checked").each(function() {     obj["dlcat_" + this.id] = true; });  //to add other params, extend object $.extend(obj, {"otherparam" : "otherparamsvalue"}); 

then in $.ajax, pass obj variable data. received params

$.ajax({     url: 'ajax/register_driver.php',     data: obj,     success: function () {      },     error: function () {         alert('data update failed. please try again!');     } }) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -