2

html

<button id="btn2">show alert array</button>
<button id="btn"> go to php</button>

javascript

var test = new Array();
test.push("one");
test.push("two");
json = JSON.stringify(test);

$('#btn').click(function(){
$.ajax({
type: "POST",
url: "json.php",
data: {data:json}
});
});

$('#btn2').click(function(){
alert(json);
});

php file (json.php)

<?php
$data = json_decode($_POST['data']);
var_dump($data);
?>

id="btn2" is working. It displays an alert with the array on it, but when I click in the id="btn", it is not working at all. Can you tell me the problem of these codes?? I just want to send an array from javascript to php file.

2
  • 1
    What's "not working"? How do you know it's not working? Commented Oct 18, 2013 at 16:20
  • 2
    That's because you've not told it to do anything... Nothing visible, at least. Commented Oct 18, 2013 at 16:23

3 Answers 3

3

You do a GET request on the client side:

type: "GET",

But you are expecting POST data on the server side:

$_POST['data']

(This answer is not true anymore because the OP edited the question.)

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

3 Comments

I already replaced the get and post method, but still the same, no response at all..
Depending on what the AJAX call is for — if OP is simply intending to retrieve/fetch data, then GET would be more appropriate (and hence requiring changing $_POST to $_GET in PHP.
@RitzdeGuzman Please var_dump($_REQUEST) and post the result.
1

how come you say it is not working at all, check your ajax function, you should have a success handler in it,

$('#btn').click(function(){
$.ajax({
type: "GET",
url: "json.php",
data: {data:json},
success:function(data){
alert(data);
}
});
});

otherwise how are you supposed to know whether the ajax happened ?

2 Comments

You could always just watch the dev console to see if the call was fired.
ohh.. i see.. that's right.. I forgot to use success for that to display the results. As simple as that. I'm just a little bit busy and forgot that little thing.
0

This is not the correct way to send an array from JavaScript to PHP. There is no need for JSON here. You can just send the array in your AJAX call. It will be serialized correctly:

$('#btn').click(function(){
    $.ajax({
        type: "POST",  // Make sure to read from the right array in PHP
        url: "json.php",
        data: {data: test} // test is your array, no JSON.stringify needed
    });
});

You are reading $_POST['data'] in PHP, so you need to send the data as POST

This has the added benefit that in PHP your $_POST['data'] will be an array! No json_decode needed.

3 Comments

He now reads and sends the data via GET (see his updated question).
@ComFreek: But he still doesn't need to use JSON, it's superfluous when he's already using query strings.
That's right, of course (I didn't say anything against that). The OP uses POST again in the question, so nevermind my first comment.

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.