1

im trying to pass an array of data to a PHP script using Jquery's AJAX; this is my JS:

var dataObject = {
    shippingDataRow: [{
        name: $('#shippingData_name').val(),
        street: $('#shippingData_street').val(),
        number: $('#shippingData_number').val(), 
    }]

};

$.ajax({
    type: "post",
    data: { action: 'test', data: dataObject },
    cache: false,
    url: "php/post_test.php",  
});

And this is the PHP (just doing some tests, before I can write all procedures),

<?php
    function test(){
        $dataObject = $_POST['dataObject'];
        print_r( $dataObject );
    }
?>

In Chrome's Developer Tools, I can see the Status Code:200 OK; even all the array with the correct values, but when I open the php it doesn't work.

Any ideas?

2
  • 3
    what is the value of var_dump($_POST); ? Commented Mar 5, 2012 at 7:25
  • Your code contains un-necessary commas, IE WILL complain! Commented Mar 5, 2012 at 7:42

6 Answers 6

1

try: $dataObject = $_POST['data'];

You need to refer to the key of the posted data, not the value in javascript.

Sign up to request clarification or add additional context in comments.

Comments

0

Your post should be

$_POST['data'];

Comments

0

You are not calling test().in anywhere.so,its cuess return 200ok but response null.echo anything in test_php.and check.

Comments

0
var dataObject = {
    shippingDataRow: {
        name: $('#shippingData_name').val(),
        street: $('#shippingData_street').val(),
        number: $('#shippingData_number').val()
    }
};

$.ajax({
    type: "post",
    data: { action: 'test', data: dataObject },
    cache: false,
    url: "php/post_test.php"
});

-

<?php
   $dataObject = $_POST['data'];
   print_r( $dataObject );
?>

Comments

0

As others mentioned, $_POST['data'] will get you the data array.

Also, using $_REQUEST works well as it'll target both get/post requests.

Comments

0

Change:

$dataObject = $_POST['dataObject'];

To:

$dataObject = $_POST['data'];

Comments

Your Answer

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.