View Full Version : External JavaScript and PHP

05-05-2006, 12:32 AM

I am using phpBB and wanted to display latest poll in phpBB to my home page.
My home page is html extention and want to use java to call it and display it.

I am stuck with these codes;
php codes;

<? php
echo "document.write('$poll['options'][$i]['vote_option_text']');";
} ?>

My question is that, if ['options'] then will not print anything, if I remove ' ' then print something. However if I put all 4 items ('$poll['options'][$i]['vote_option_text']');"; without ' ' inside of [ ] it will print like;


What is correct syntax?
Please help!!!

Thank you in advance

05-05-2006, 06:45 AM
If the PHP is in an external Javascript file you need to specify the scipt tag as

<script type="text/javascript" src="myfile.php"></script>

so that it gets parsed as PHP on the server first before being run as Javascript by the browser.

05-06-2006, 01:50 AM
Ooops, I may be in wrong forum. :)

Thank you very much 'felgall'.

I may didn't make it clear.
I've already did that part in my html page.
Also I changed my php file as below;

Header("content-type: application/x-javascript");

function myfunction()
return $something;


echo "document.write('$poll['options'][$i]['vote_option_text']');";


When called remotely by javaScript, I included this in my html page as below;

<script> type="text/javascript" src="http://www.mydomain.com/myfile.php"></script>

But then problem is as my first post.
Help please.

Thank you in advance