jwwceo Posted March 19, 2008 Share Posted March 19, 2008 Hello! I am trying to make a little widget type script for a sidebar in WordPress. I would like to include a DIV from one server onto another server the source code is in php however. Is there anyway to include php code as the source for a snippet of javascript?? James Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 19, 2008 Share Posted March 19, 2008 what you can do is call the php file on the other server through the src attribute of a script tag: <script src="http://otherdomain.com/jscript.php"></script> what that jscript.php file consists of is a bunch of javascript document.writes with php code mixed in with it: <?php // jscript.php // set my mime header header('Content-type: text/javascript'); // stepping out of php now ?> document.write("myname=<?php echo 'test'; ?>"); --the general idea is that you mix in php with javascript to insert values recieved from php into the js. Only js will actually be sent to the browser where it will then be executed and the document.writes will write out some html like a div in the page at that point. This method is actually the method used by google for it's google gadgets and is the method used by many other web widgets. Quote Link to comment Share on other sites More sharing options...
jwwceo Posted March 20, 2008 Author Share Posted March 20, 2008 so if my php page is more than just text outputs...it has queries, etc. How do I wrap the whole php file in on of those document.write tags?? James Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 20, 2008 Share Posted March 20, 2008 queries operate on the server side not the client side. you can output the results of the query into the document.write statements: <?php // jscript.php // set my mime header header('Content-type: text/javascript'); // stepping out of php into javascript now // i'm gonna wrap everything in a div ?> document.write('<div>'); <?php // some open db code here $result = mysql_query($sql); // loop through result while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // stepping out of php into javascript now ?> document.write("rowvalue=<?php echo $row['columnname'] . '<br>'; ?>"); <?php } // end while loop //close the div ?> document.write('</div>'); --the thing to understand is that the php executes on the server side and can do anything php normally does including accessing dbs. However, just like when php is mixed with html, the php is used to variably output html and then that is sent to the browser and interpreted. In this case you use php to variably output javascript which is then interpreted by the browser. Because you are calling a php page on another server, ajax would not work for you and the only other option to doing what you want besides the above is to make an iframe in your calling page. Quote Link to comment Share on other sites More sharing options...
jwwceo Posted March 20, 2008 Author Share Posted March 20, 2008 thanks a bunch. So it's basically like an echo, where we output the php within the javascript... I will test it out!! James Quote Link to comment Share on other sites More sharing options...
mainewoods Posted March 21, 2008 Share Posted March 21, 2008 yup , you're just using php to echo values into js just like you do with html. if the js doesn't work as you expected, try entering the url of the script src attribute into your browsers address field directly and then you will be able to view source in your browser and see what code it's actually returning and if it looks like valid js. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.