1

I am trying to send an array from PHP to Ajax in javascript. my array in PHP file "myphp.php" is $parts and is multidimentional.

I have used both echo $parts and echo json_encode($parts) none works for me. $parts has 2 rows and 2 columns this is the result of var_dump:

array(4) { [0]=> array(3) { [0]=> string(13) "1350655763.12" [1]=> string(2) "25" [2]=> string(1) " " } [1]=> array(3) { [0]=> string(13) "1350655763.12" [1]=> string(2) "26" [2]=> string(0) "" } [2]=> array(1) { [0]=> string(0) "" } [3]=> array(1) { [0]=> string(0) "" } }

I want to send the hole array to Ajax but just display $parts[0][0] on the web page. When I use

echo $parts 

in myphp.php file: "Array" name is displayed on the web page When I use

echo json_encode($parts) 

in myphp.php file: all components of the array are displayed instead of the first one

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <script>
function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest()
{
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4) 
     { 
      var parts=JSON.parse(xmlHttp.responseText); 
      document.getElementById('ResponseDiv').innerHTML = parts[0][0]; 
     }
  }

  xmlHttp.open("GET", "myphp.php", true); 
  xmlHttp.send(null);
}


result=MakeRequest();



</script>


  </head>
  <body>

    <div id='ResponseDiv'>
      This is a div to hold the response.
    </div>
  </body>
</html>

How should I change these codes to just get parst[0][0] on the webpage? Thanks in advance for you help

2 Answers 2

1

you need to use JSON.parse on at javascript and json_encode at php.

PHP:

$parts = array();
....
echo (json_encode($parts));

Javascript:

var parts = JSON.parse(xmlHttp.responseText);

Optionally, on your PHP page, you can also define header('Content-type: application/json');

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

8 Comments

which part of code in javascript I need to change? I used var parts = JSON.Parse(result) and then document.getElementById('ResponseDiv').innerHTML = parts[0][0] I does not work !
the part where you are getting response from the ajax. In your code, its HandleResponse(xmlHttp.responseText); line. Replace this line by var parts = JSON.parse(xmlHttp.responseText);alert(parts); and rest you can understand OR can ask me again here.
I changed it to "HandleResponse(JSON.parse(xmlHttp.responseText));" but it does not work even now
Please remove the HandleResponse. I don't know where have you declared that function. Just replace HandleResponse(xmlHttp.responseText); by var parts=JSON.parse(xmlHttp.responseText);alert(parts[0][0]);
I changed one part of code as you said but still does not work (I put that part betwen stars //*** so you can find) could you please take a look at that part
|
0

You should parse your json encoded data into javascript before use it. Please check json here

http://www.json.org/js.html

Please try this code:

result = MakeRequest();
var myObject = eval('(' + result + ')');
alert(myObject[0][0]);

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.