php - How to use chekbox with Jquery ajax -


i trying use checkbox ajax. without ajax have handle it. here jquery code

$("#skillcat").click(function(e) {     var submit_val = new array();     e.preventdefault();      var ids = $('check_subjects input:checked')            .map(function(){              return this.value;            }).get();      $.ajax( {     type : "post",     datatype : "json",     url : "./wp-admin/admin-ajax.php",     data : {         action : 'each_category',         check_subjects : ids     },     success : function(data) {         alert(data);          $('#accordion').html(data);       }     }); }); 

here php code button , checkbox generated in serverside

foreach($subjects $key=> $data){      echo '<input type="checkbox" id="'. $data->id .'" value="'. $data->id .'" name="check_subjects[]">&nbsp;&nbsp;  '. $data->subject .'<br>'; } 

serverside used catch data post array

if(isset($_post['check_subjects'])){     //var_dump(check_subjects);     $check_subjects  = implode(',', $_post['check_subjects']);     //echo '$check_subjects&nbsp;&nbsp;&nbsp;&nbsp;'. $check_subjects; } 

however when run above codes, notice data sent server(via chrom developer tool). null in alert box. guess response null becase data not sent server properly. not sure javasript code use passe request ajax.

can explain have done mistake?

try this:

$("#skillcat").on('click', function (e) {     e.preventdefault();      var ids = [];     var $el = $('[name*=check_subjects]:checked'); //get checked checkboxes     $el.each(function () {         ids.push($(this).attr('id')); //get id each checkbox     });      $.ajax({         type: "post",         datatype: "html",         url : "./wp-admin/admin-ajax.php",         data: {             action: 'each_category',             check_subjects: ids         },         success: function (data) {             alert(data);             $('#accordion').html(data);         }     }); }); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -