$('#button').live('click', function() {
   values_array = [];
   $('.field').each(function() {
      values_array.push = $(this).val();
   });
   $.ajax({
      url: 'page.php', 
      data: { array_a : values_array },
      type: 'POST',

      success: function(data) {
         $('#div').html(data);
      }
   });
});


//page.php
echo $_POST['array_a'] . "<br/>"; //The line break echos, but nothing else

A.) Do I need to iterate through each class with $.each in order to create a proper array, and

B.) Why doesn't php echo it?

share|improve this question

Did you tried to put array_a in brackets - data: { "array_a" : values_array }? – Karo May 13 at 11:43
Just tried it. No luck. – Norse May 13 at 11:44
What is the output of print_r($_POST) ? – Karo May 13 at 11:45
Array ( ), as expected – Norse May 13 at 11:47
check out what's the content of values_array after each() with console.log() – Christian Koch May 13 at 11:47
feedback

2 Answers

up vote 5 down vote accepted

Change:

values_array.push = $(this).val();

to:

values_array.push($(this).val());

That should do the trick :)

share|improve this answer
Perfect thanks. – Norse May 13 at 11:56
feedback

.push is a method which you have used like a property try instead

values_array = [];
   $('.field').each(function() {
      values_array.push($(this).val());
   });

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.