Javascript Including Php

Discussion in 'JavaScript' started by geekazoid, Aug 29, 2006.

  1. #1
    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
     
    geekazoid, Aug 29, 2006 IP
  2. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #2
    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.
     
    noppid, Aug 29, 2006 IP
  3. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so all i have to do is save my php file as .js is that what your saying?
     
    geekazoid, Aug 29, 2006 IP
  4. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    Darrin, Aug 29, 2006 IP
  5. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    im sorry i didnt understand that... would it be possible to make that a bit clearer im a bit thick . sorry
     
    geekazoid, Sep 7, 2006 IP
  6. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Your PHP script would look like this:

    
    <?php
    
    // generate random quote $random_quote (from database / file)
    
    echo "document.write('" . str_replace("'", "\'", $random_quote) . "');";
    
    ?>
    
    PHP:
    Output:

    
    document.write('Example \'quote\'.');
    
    Code (markup):
    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>
    
    Code (markup):
     
    SoKickIt, Sep 11, 2006 IP
  7. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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 ?
     
    geekazoid, Sep 12, 2006 IP