javascript - Ajax - Manipulate a correct array to pass to PHP -
hi guys new jquery , have encountered type of problem,
i trying create array jquery pass on php controller (ci).
sample html
<input type="text" acc_id="5" class="input" /> <input type="text" acc_id="10" class="input" /> <input type="text" acc_id="15" class="input" /> <input type="text" acc_id="20" class="input" />
javascript
$('#save').click(function(){ var arrayval = []; $('.input').each(function(key, value){ var id = $(this).attr('acc_id'); var inputvalue = $(this).val(); arrayval[id] = inputvalue; }); $.ajax({ url: siteurl + '/sample_save', type: 'post', datatype: 'json', data: {'accounts' : arrayval}, success: function(data) {} }); });
here response in php
array ( [array] => array ( [0] => [1] => [2] => [3] => [4] => [5] => value 1 [6] => [7] => [8] => [9] => [10] => value 2 [11] => [12] => [13] => [14] => [15] => value 3 [16] => [17] => [18] => [19] => [20] => value 4 ) )
notice response gave me key 0 - 20.
but want :
array ( [array] => array ( [5] => value 1 [10] => value 2 [15] => value 3 [20] => value 4 ) )
i can somehow make work in php, want correct stuff through javascript code. sorry bad english :)
you right. should solved within js code.
jquery has native serialize function forms. see here example use it, may better approach problem.
also please aware javascript not have real associative arrays. able use objects in similar way know associative arrays other languages (like php).
edit
ok, in case code ok. need use object instead of array.
just use:
var arrayval = {};
instead of:
var arrayval = [];
Comments
Post a Comment