0
$('#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?

thecodedeveloper.com
3,2505 gold badges40 silver badges69 bronze badges
asked May 13, 2012 at 11:40
4
  • Did you tried to put array_a in brackets - data: { "array_a" : values_array }? Commented May 13, 2012 at 11:43
  • What is the output of print_r($_POST) ? Commented May 13, 2012 at 11:45
  • Array ( ), as expected Commented May 13, 2012 at 11:47
  • check out what's the content of values_array after each() with console.log() Commented May 13, 2012 at 11:47

2 Answers 2

6

Change:

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

to:

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

That should do the trick :)

answered May 13, 2012 at 11:47
Sign up to request clarification or add additional context in comments.

Comments

1

.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

answered May 13, 2012 at 11:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.