Hi guys i am new to jQuery and I have encountered this type of problem,
I am trying to create an array from jquery to pass it on to 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 is my 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 that the response gave me the key from 0 - 20.
But I want it like this :
Array
(
[array] => Array
(
[5] => value 1
[10] => value 2
[15] => value 3
[20] => value 4
)
)
I can somehow make it work in Php, but I want to correct this stuff through my javascript code. Sorry for my bad english :)