-3

I would like to send array to the server.

This is how i try to send the array:

jQuery(document).ready(function($){
    $.ajax({
        type: "POST",
        url: "file.php",
        datatype : "json",
        data : JSON.stringify({ name: "Daniel", phone: "01234123456" }),
        success: function(msg){ 
            alert('Success!');
        }
    });
});

This is how i try to get the array in file.php

print_r($_POST);
print_r($_GET);
print_r(json_decode($_POST);

Of course in the firebug console i see the array but not on the page.

2
  • How are you testing this? i see the array but not on the page. Commented Nov 11, 2013 at 19:36
  • 1
    BTW you never print the result on to the screen - you have to, in the success callback function, add them to the DOM, otherwise they will not just apear there by magic Commented Nov 11, 2013 at 19:41

1 Answer 1

0

You are currently sending the data - what you refer to as the 'array' in the format of JSON - java script object notation.

It is not possible to send data across (at least in jQuery ajax) in JSON. You must stringify it before you send it, and then decode it in the php end (like you already do - got that part right). You would have to do something like:

  data: JSON.stringify({ name: "Daniel", phone: "01234123456" }),

Hope this helps!

1
  • jQuery does this automatically by default. If data contains an object, it will be urlencoded and the request header Content-Type: application/x-www-form-urlencoded will be set. In OP's case the data would already be available in $_POST['name'] and $_POST['phone'] in PHP. Commented Nov 11, 2013 at 19:57

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.