2

Hi i am trying to send Json via Ajax to PHP using Javascript. The data is sent correctly by index.html when I view it on firebug. It shows Json type with the correct data. However, it seems that I am unable to read the JSON on php. I am unable to access it using $_POST. I tried using $_POST['name'] and there is not response. When i try using $_POST, the response is array. Can you please help me?

Here is my javascript code.

<html>
<head>
<script type="text/javascript">
    //Create Http request depending on browser
    var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();}
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}

    xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;}
      }

    // Function to create 
    var url = "control.php";
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    var data=JSON.stringify({"name":"John", "time":"2pm"});
    xmlhttp.send(data);
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>

This is my php code

<?php
include_once('JSON.php');   
$json = new Services_JSON();
$value = $json->decode($_POST['name']);
echo $value;
?>

Have been working on this for days and I really appreciate any help that you can offer. Thank you!

3
  • why not use a javascript library? Commented May 3, 2012 at 5:53
  • Use jQuery.ajax(); and if you experience problems with the request investigate with Firebug addon for Firefox (Tab Console or Network) Commented May 3, 2012 at 5:56
  • Do print_r($_POST) and you will see what is your $_POST global variable. Maybe it will help you to solve the problem Commented May 3, 2012 at 6:02

3 Answers 3

2

here it's:

print_r($GLOBALS['HTTP_RAW_POST_DATA']);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey! thanks so much! This solved the problem for me. Can you point me to a documentation where i can read up on why $GLOBALS['HTTP_RAW_POST_DATA'] worked in this case? But nevertheless thank you guys so much! You just made my day=)
you can find it here PHP Manual.But i will make some test like(print_r($GLOBAL),print_r($_SERVER)) when i have problem like this.That is helpful. :)
1

I think that it needs to parse the whole post first.

<?php
include_once('JSON.php');   
$json = new Services_JSON();
$value = $json->decode($_POST);
echo $value;
?>

But also do you need these includes? http://www.php.net/manual/en/function.json-decode.php

include_once('JSON.php');   
$json = new Services_JSON();

can you not just do this?

echo json_decode($_POST)

1 Comment

Hi thanks for all the comments and answers! I tried using just echo json_decode($_POST) as you suggested but the response given is blank. When i try to do print_r($_POST), the given output is arrray().
0

An even better solution (see here) is use:

$json = json_decode(file_get_contents("php://input"), true) ?: [];
print_r($json);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.