javascript - How to send data in 2 dimensional array with ajax to the php page and print each dimension separately? -


so,i have made page has multiple checkboxes. fetching data check boxes, storing data in 2dimensional array jquery. code looks this,

htmlpage,

<input type="checkbox" name="checkboxs[]"  price="12" value="something1"> <input type="checkbox" name="checkboxs[]"  price="15" value="something2"> <input type="checkbox" name="checkboxs[]"  price="16" value="something3">  <div id='answer'></div> 

... jq

var data =[];  .find("input[type='checkbox']:checked").each(function (){    data.push($(this).val(),$(this).attr('price'));    });   saving in data when consoled this,  output :["corn bhel","15","corn tikki chat","15","corn dahi puri","15"] 

sending ajax.,

$.ajax({      type:'post',      url:'check.php',      datatype: 'html',      data:'data='+data,      success: function(data){          $('.answer').html(data)      }  }); 

so output expect should this

they both should in different array in php page.

enter image description here

if want receive result

$prices = [15,15]; $goods = ["corn bhel","corn tikki chat"]; 

you may way:

var prices = []; var goods = [];  // push prices , values different arrays $.each($.find("input[type='checkbox']:checked"),function(){   goods.push($(this).val());   prices.push($(this).attr('price')); })  $.ajax({   type:'post',   url:'http://127.0.0.1:8888/1.php',   datatype: 'html',   // send data 2 different properties   data:{prices:prices,goods:goods},   success: function(data){      $('.answer').html(data)   }   }); 

and on backend receive in $_post:

<?php  $prices = $_post['prices']; $goods = $_post['goods'];  print_r($prices); print_r($goods); 

test:

array (     [0] => 15     [1] => 16 ) array (     [0] => something2     [1] => something3 ) 

but on way may have troubles linkage good<->price on client side (if price or empty, example), better way aggregate request two-dimensional array:

var items = [];  $.each($.find("input[type='checkbox']:checked"),function(){   items.push({     good:$(this).val(),     price:$(this).attr('price')   }); })  $.ajax({   type:'post',   url:'http://127.0.0.1:8888/1.php',   datatype: 'html',   data:{items:items},   success: function(data){      $('.answer').html(data)   }   }); 

in case can read array of "models" on server side:

<?php print_r($_post); 

test:

array (     [items] => array         (             [0] => array                 (                     [good] => something2                     [price] => 15                 )              [1] => array                 (                     [good] => something3                     [price] => 16                 )          )  ) 

this variant more safe because work ready 'models', not raw set of properties.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -