+ Reply to Thread
Results 1 to 7 of 7

Thread: help putting PHP in my JAVASCRIPT

  1. #1
    ryanmm is offline x10Hosting Member ryanmm is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    help putting PHP in my JAVASCRIPT

    First off, yes I know php only runs server side and only once before the page is output. That said, this should still work. What am I doing wrong?

    MP3COMPETEjs.php (my JS file):

    Code:
    Header("content-type: application/x-javascript");
    
    //-----------blinking text
    
    function blinkFont(obj)
    {
      document.getElementById("obj").style.color="red";
      setTimeout("setblinkFont(obj)",1000);
    }
    function setblinkFont(obj)
    {
      document.getElementById("obj").style.color="<?PHP echo $color; ?>";
      setTimeout("blinkFont(obj)",1000);
    }
    //----------end blinking text

    PHP/HTML (filename: index.php):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript" src="../Javascript/MP3COMPETEjs.php"></script>
    </head>
    <body>
    
    <?PHP
    
    //For easier understanding, lets set $values and $color here.
    //Though the whole reason i need to do this is because 
    //$values and $color are dynamic and based on
    //data retrieved from a MySQL database.
    $values = 0;
    $color = '#0033FF';
    
    if(time() - $values > 147600)
    {
        echo '<script type="text/javascript">blinkFont(blinkThis);</script>';
        echo '<a id="blinkThis" href="CompSummary.php">' . $values . '</a>';
    }
    
    ?>
    
    </body>
    </html>
    Last edited by ryanmm; 04-06-2011 at 02:56 PM.

  2. #2
    adam.k is offline x10Hosting Member adam.k is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    35

    Re: help putting PHP in my JAVASCRIPT

    Hi ryanmm,

    Are you getting any errors about an undefined variable: color?.

    Or better question,

    why does it have to be PHP?
    Last edited by adam.k; 04-06-2011 at 03:32 PM.

  3. #3
    ryanmm is offline x10Hosting Member ryanmm is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    Re: help putting PHP in my JAVASCRIPT

    nope, no errors. The link just isnt changing from red to anything and then back, like the JS is supposed to make it do.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: help putting PHP in my JAVASCRIPT

    Don't use blinking text; it's terrible for usability. The BLINK element was originally a bad joke. Read on for solutions to the problems with the sample code not to fix blinking text but to fix your skills.

    Note the generated HTML content:
    HTML Code:
    <script type="text/javascript">blinkFont(blinkThis);</script>
    <a id="blinkThis" href="CompSummary.php">0</a>
    Take a look at your error console and you'll see the problem (well, two problems) with this.

    If that's a representative sample of MP3COMPETEjs.php, then you're "header" call is in a JS region, not PHP.

    The MIME type for JS is "application/javascript". IE ≤ 8 has problems with this MIME type in "type" attributes, but not with the Content-type header. IE may have problems with the "php" extension. To fix this, use clean URLs (based on rewrite rules or content negotiation; web and forum searches will turn up specifics) to hide the "php" extension.

    Note in blinkFont and setblinkFont you're getting the element with the ID of "obj", rather than the ID stored in variable obj, due to the quotes.

    setTimeout evaluates strings in global context. The obj you want to pass to blinkFont and setblinkFont is a local variable, not a global variable, so a string argument won't work. For this reason (among others), it's better to pass a function rather than a string to setTimeout and its cousin, setInterval.

    MP3COMPETE.js.php:
    PHP Code:
    <?php header("Content-type: application/javascript"); ?>
    //-----------blinking text
    function blinkFont(obj) {
        // note obj, not "obj"
        document.getElementById(obj).style.color="red";
        // You can pass a function rather than a string.
        setTimeout(function () {setblinkFont(obj);}, 1000);
    }
    function setblinkFont(obj) {
        document.getElementById(obj).style.color="<?PHP echo $color?>";
        setTimeout(function () {blinkFont(obj)}, 1000);
    }

    // alternate implementation
    function blinkFont(obj) {
        var elt = document.getElementById(obj);
        var blinker = function () {
            if (elt.style.color == 'red') {
                elt.style.color = '<?PHP echo $color?>';
            } else {
                elt.style.color = 'red';
            }
        }
        return setInterval(blinker, 1000);
    }
    //----------end blinking text
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <script type="text/javascript" src="../script/MP3COMPETE.js"></script>
      </head>
      <body>
        <?php
          //For easier understanding, lets set $values and $color here.
          //Though the whole reason i need to do this is because 
          //$values and $color are dynamic and based on
          //data retrieved from a MySQL database.
          $values = 0;
          $color = '#0033FF';
          
          # Note the following still has two bugs.
          if(time() - $values > 147600) { ?>
              <script type="text/javascript">blinkFont(blinkThis);</script>
              <a id="blinkThis" href="CompSummary.php"><?php echo $values ?></a>
          <?php }
        ?>
      </body>
    </html>
    As always, questions like this should include a link to a live page. Use [php] or [html] tags over [code] when appropriate. You'll get syntax hilighting that can reveal typos. Code should be indented for readability.
    Last edited by misson; 04-07-2011 at 04:00 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    ryanmm is offline x10Hosting Member ryanmm is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    Re: help putting PHP in my JAVASCRIPT

    thanks misson

  6. #6
    ryanmm is offline x10Hosting Member ryanmm is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    Re: help putting PHP in my JAVASCRIPT

    Would you explain this to me:

    The MIME type for JS is "application/javascript". IE ≤ 8 has problems with this MIME type in "type" attributes, but not with the Content-type header.

    I dont quite follow what you are saying. I used the application/javascript header. Youre saying thats good or bad?

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: help putting PHP in my JAVASCRIPT

    Quote Originally Posted by ryanmm View Post
    Would you explain this to me: [...]

    I dont quite follow what you are saying. I used the application/javascript header. Youre saying thats good or bad?
    You used a MIME type of "application/x-javascript", which is not the current standard value and not recommended. "application/javascript" doesn't work in IE ≤ 8 when used as the value of a "type" attribute of a <script> tag:

    Code:
    <script type="application/javascript">
    /* IE ≤ 8 won't evaluate this */
    function important() {
        ...
    }
    </script>
    For this reason, it's best to use "text/javascript" as the type attribute for now. The Content-type header, however, doesn't suffer from this problem, so "application/javascript" should be used in the Content-type header for javascript resources.
    Last edited by misson; 04-07-2011 at 03:25 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. putting ads
    By aerojp08 in forum Free Hosting
    Replies: 5
    Last Post: 10-06-2009, 09:31 AM
  2. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  3. Putting Ads in SMF
    By Chirantha in forum Free Hosting
    Replies: 1
    Last Post: 09-06-2005, 07:17 AM
  4. Putting Ads in IPB 2.0?
    By tehpwner in forum Free Hosting
    Replies: 6
    Last Post: 08-30-2005, 10:38 AM
  5. I need help putting ads
    By sportman3333333 in forum Free Hosting
    Replies: 20
    Last Post: 02-07-2005, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers