php - Pass JS Array to server through $.ajax call -
here piece of code :
$('#addtype').on('submit', function(e) { $form = $(this); var type = []; $(this).find('input[type=number]').each(function() { type[$(this).attr('id')] = $(this).val(); }); console.log(type); $.ajax({ type: 'post', url: '<?php echo $this->url(array(), 'addtype');?>', data: {type: type}, success: function() { $form.find('input[type=submit]').attr('disabled','disabled'); } }); e.preventdefault(); });
as can see, builds javascript array input
s in form#addtype
, send server side script supposed handle array. problem no data passed through $.ajax({})
.
update
it seems comes values of array keys cannot litteral. if put incremented number key, array passed. how come?
make type
object, not array:
var type = { };
you created type
array (using []
). array object, too, can add named members it, in loop:
type['my-item-id'] = xxx;
but when add type
variable ajax request, jquery checks variable array , iterates through type
trough array, looks it's "numerical-indexed" items using type.length
(which 0 added none):
for (var = 0; < type.length; i++) ... type[i] ...
when create type
object (using {}
), jquery sees should iterate on type
on object , , "sees" "named" items:
for (var in type) ... type[i] ...
Comments
Post a Comment