Converting PHP XPathNodeList to javascript array?

430

I have a small PHP script I have written that gets reviews from a website. However, I am trying to display them in Javascript but cannot get the values through to the JS.

in PHP I have the variable $reviewTitles populated using:

    $reviewTitles = $xpath->query("//*[@class='" . $reviewStr . "']");

and I can access each value in PHP by using

    $title = $reviewTitles->item($x)->nodeValue;

Where $x is the index of the title to display.

My question is how can I convert whatever object the php variable $reviewTitles holds into javascript, So I am able to access each title via a JavaScript array.

I have tried:

    <script language="JavaScript" type="text/javascript"> 
    var titles = '<?php echo json_encode($reviewTitles); ?>';
    var titles_data = JSON.parse(titles );
    window.alert(titles_data[0]);
    </script>

But get nothing - What am I doing wrong?

Thanks for taking the time to read this.

People are also looking for solutions to the problem: php - Trouble with session variables

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.