How do I call a random quote using PHP and javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RTB
    New Member
    • Feb 2007
    • 4

    How do I call a random quote using PHP and javascript?

    I've been trying to replace my old PHP/iframe random quotes script with a PHP/javascript without success.



    <script scr="http://www.just-quotes.com/wow/random.php" type="text/javascript"></script>

    The script "random.php " works correctly and pulls a random quote from the text file but everything I've tried fails to produce a quote in the HTML page.

    Help is needed!
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    Originally posted by RTB
    I've been trying to replace my old PHP/iframe random quotes script with a PHP/javascript without success.

    http://www.just-quotes.com/wow/random.php

    <script scr="http://www.just-quotes.com/wow/random.php" type="text/javascript"></script>

    The script "random.php " works correctly and pulls a random quote from the text file but everything I've tried fails to produce a quote in the HTML page.

    Help is needed!
    With PHP in your HTML page you need to include the script like this:
    Code:
    <? include('some_file.php'); ?>
    If your script displays something it will show where you include the php file. If I am missing something let me know we can move this to the PHP forum.

    Aric

    Comment

    • RTB
      New Member
      • Feb 2007
      • 4

      #3
      Originally posted by AricC
      With PHP in your HTML page you need to include the script like this:
      Code:
      <? include('some_file.php'); ?>
      If your script displays something it will show where you include the php file. If I am missing something let me know we can move this to the PHP forum.

      Aric

      This script will also be used externally from other sites.

      RTB

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        It's difficult to replicate the behaviour without seeing the PHP code, but you can try the following:
        1) Get the quotes from the PHP (a database I assume).
        2) Put them in a Javascript array, quotes.
        3) Then use Math.random, e.g:
        Code:
        index = Math.floor(Math.random() * quotes.length);
        4) Use index to get your quote:
        Code:
        document.write(quotes[index]);
        and you're done!

        Comment

        • RTB
          New Member
          • Feb 2007
          • 4

          #5
          Originally posted by acoder
          It's difficult to replicate the behaviour without seeing the PHP code, but you can try the following:
          1) Get the quotes from the PHP (a database I assume).
          2) Put them in a Javascript array, quotes.
          3) Then use Math.random, e.g:
          Code:
          index = Math.floor(Math.random() * quotes.length);
          4) Use index to get your quote:
          Code:
          document.write(quotes[index]);
          and you're done!
          The quotes are in a text file and the script is also used by other web sites.

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            why not use an iframe

            Comment

            • RTB
              New Member
              • Feb 2007
              • 4

              #7
              Originally posted by iam_clint
              why not use an iframe
              The iframe has fixed sizing and I needed to have it more flexible.

              I've since got it working much better.



              Code:
              <center><table width="100%"><tr><td>
              <script type="text/javascript" src="http://www.just-quotes.com/wow/random_quotes.php"></script>
              <noscript>Your browser does not support JavaScript!</noscript>
              <div align="right">By <a href="http://www.just-quotes.com" target="_blank">Just-Quotes.com</a></div>
              </td></tr></table></center>
              Last edited by RTB; Mar 10 '07, 08:15 PM. Reason: Typo

              Comment

              Working...