0

I have the following code wherein I'm trying to get values of the xml element 'point' from an RSS feed using PHP. When I display the value of point using 'echo', it gives me the correct data. However, when I pass the same parameter to a javascript function, it calls the function very randomly. The alert in the javascript function gives me the sum of the points of either 2 or 3 point values from each iteration. How do I resolve this problem?

<!DOCTYPE html>
<html>
<head>
    <title>Natural Disasters</title>

    <script type="text/javascript" src="http://maps.google.com/maps/api/js? sensor=false&amp;libraries=places"></script>
    <script type="text/javascript">
        var i=0;
        function getPoint(point) {

            alert("Value of i : "+i+" Value of point : "+point);
            ++i;
        }
    </script>
    <script>
        function showRSS(str) {
            if (str.length === 0) {
                document.getElementById("rssOutput").innerHTML = "";
                return;
            }
            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("rssOutput").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET", "getrss.php?q=" + str, true);
            xmlhttp.send();
        }

    </script>
</head>

<body>
    <?php
    $xml2 = ("http://www.volcano.si.edu/news/WeeklyVolcanoRSS.xml?");
    $xmlDoc2 = new DOMDocument();
    $xmlDoc2->load($xml2);
    $x2 = $xmlDoc2->getElementsByTagName('item');
    $i2 = 0;

    foreach ($x2 as $y) {

        $geo_rss = $y->getElementsByTagNameNS("http://www.georss.org/georss", 'point');
        $title = $y->getElementsByTagName('title')->item(0)->nodeValue;
        $point = $geo_rss->item(0)->nodeValue;
        echo ("<h3>" . $title . "</h3>" );
        echo ($point);

        echo '<script type="text/javascript">'
        , 'getPoint(' . $point . ');'
        , '</script>'
        ;
        ++$i2;
    }
    ?>
</body>
</html>

P.S. :I have followed the same procedure for another set of parameters, and it works perfectly fine. I do not know why this one is not working.

1
  • please clarify your question. which function will be called upon for that iteration? Commented Mar 27, 2015 at 8:09

1 Answer 1

1

Change your php code to write script as below :

echo '<script type="text/javascript"> getPoint("' . $point . '");</script>';

You just need to wrap $point into double quotes.

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

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.