Jump to content

Echo PHP in javascript


ricktee76

Recommended Posts

I'm trying to echo a php array into the javascript, does anyone have any ideas as how it can be done?

 

function showItemOfTheDay()
    {
        var xmlDocument = request.responseXML;
        var nodes = xmlDocument.selectNodes( '//item' );
        var numberOfItems = nodes.length;
        var where = '[color=red][b]<?PHP echo "$msstrip"; ?>[/b][/color]';
        var randomNumber = getRandomNumber( numberOfItems );
        var selectedNode = nodes[randomNumber];
        var thumbnail = selectedNode.selectSingleNode( 'media:thumbnail//@url' );
        var title = selectedNode.selectSingleNode( 'title' );
//        var link = selectedNode.selectSingleNode( 'link' );
        divLink.innerHTML = '<img src="' + thumbnail.text + '" width="90" height="70" alt="' + title.text + '" border="0"/></a>';
//    divLink.innerHTML = '<a href="' + link.text + '" target="_blank"><img src="' + thumbnail.text + '" width="90" height="60" alt="' + title.text + '" border="0"/></a>';
    }
    
    // Eventhandler, will be fired for every state change on our XMLHttpRequest.
    function request_onReadyStateChange()
    {
        var status = request.readyState;
        divStatus.innerText = getStatusDescription( status );
        if ( status == 4 )
        {
            showItemOfTheDay();
        }
    }

function UpdateGadget()
    {
        var url = 'http://www.awebsite.com/things/are/here/page=[b][color=red]+ where +[/color][/b]';
        request = new XMLHttpRequest();
        request.onreadystatechange = request_onReadyStateChange;
        request.open("GET", url);
        request.send();
        
        window.setTimeout(UpdateGadget, (1000 * 60 * 60 ));
    }
    
    
    // Eventhandler, will fire when the body of our gadget has completed loading.
    function Page_onLoad()
    {
        divStatus.style.display = "none";
        UpdateGadget();
    }

    </script>
</head>
<body onload="Page_onLoad();">
<g:background>
  <img id="imgNFLlogo" src="NFL-corner.png" />
  <div id="divLink"></div>
  <div id="divStatus"></div>
  </g:background>

Link to comment
https://forums.phpfreaks.com/topic/95101-echo-php-in-javascript/
Share on other sites

use PHP to write the js code

 

<?php
$array = array('a', 'b', 'c');
$items = '"' . join ('","', $array) . '"';
echo "<script language='javascript'>\n";
echo "var jsarray = new Array (";
echo $items;
echo ");\n";
echo "for (var i=0; i<3; i++) document.write(jsarray[i]);\n";
echo "</script>\n"; 
?>

 

Gives-->

<script language='javascript'>
var jsarray = new Array ("a","b","c");
for (var i=0; i<3; i++) document.write(jsarray[i]);
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.