View Full Version : Javascript Including Php
geekazoid
Aug 29th 2006, 11:55 am
Ok i have a php file which when refresh it changes the quote
http://www.geekazoid.co.uk/QOTDS.php
I would like to put this in javascript so other websites can use on there pages.
Yes the <?php include > Thing would work but i dont want them to have to use php and i know javascript works in .html so is there anyway i can do this .... ?
I know this website does it >>
http://www.quotedb.com/random_quotes_generator
So does anyone know how to do this ?
Thanx alot :D
noppid
Aug 29th 2006, 12:04 pm
The php file you point your JS at must output JS. So, you'd have to re-code your PHP file. All the current output from your PHP file needs to be in an html file, for instance, and where you want the quote to appear, you put the JS that calls the php file that outputs the JS to write your quote.
In short, yes, you can do that.
geekazoid
Aug 29th 2006, 1:14 pm
so all i have to do is save my php file as .js is that what your saying?
Darrin
Aug 29th 2006, 2:26 pm
In looking at the example web site you gave that is doing this function now, they are adding a reference to a php page that will output javascript.
What you would need to do is make a page in PHP, but instead of it's output being html with the quote, it would have to output what the content of a .js file would look like, with the javascript inside. You may have to remove some headers from the output.
What you might want to do is make a static .js file that puts out just one quote - the same one every time. Then, create a page with the include script that you are going to give out, and include that js file. Once you get it to work, create a dynamic PHP page that makes an output exactly like your .js file, except with different quotes in it each time.
Then change your include text that you give out to people to reference that PHP file instead of the static .js file that you had.
Hope that makes sense.
geekazoid
Sep 7th 2006, 11:47 am
im sorry i didnt understand that... would it be possible to make that a bit clearer im a bit thick . sorry
SoKickIt
Sep 11th 2006, 4:38 am
Your PHP script would look like this:
<?php
// generate random quote $random_quote (from database / file)
echo "document.write('" . str_replace("'", "\'", $random_quote) . "');";
?>
Output:
document.write('Example \'quote\'.');
and that's basically Javascript file.
After that you can use it on other pages:
<script language="javascript" src="http://www.geekazoid.co.uk/QOTDS.php"></script>
geekazoid
Sep 12th 2006, 12:48 am
Brilliant that looks perfect, im going to make myself look stupid here but what do i do with that script ?
I mean how do i use it... do i edit the top bit ?
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.