Javascript in PHP

Discussion in 'PHP' started by zdestiny, May 14, 2006.

  1. #1
    Can u insert a java script into php ? I am inserting an image that shows the status of the live help desk.
    I tried it and an error shows up - unexpected string.
     
    zdestiny, May 14, 2006 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sure.. watch your quotes though.

    If you are using echo/print

    echo "<script language=javascript>";

    then you have to escape any "quotes" in your script with \"
     
    TheHoff, May 14, 2006 IP
  3. zdestiny

    zdestiny Peon

    Messages:
    138
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Im editing the template for Noah's Classifieds. at the beginning of every line there's
    $s.=
     
    zdestiny, May 14, 2006 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,826
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #4
    PHP outputs text, whether that's html, xml, csv or javascript.

    The PHP has to be right, then view the source see if that's right too. Use firefox's awesome debugger

    $s.=

    just means append this onto the string.

    that error you are getting means that PHP can't use the text it's getting as text - it doesn't give a damn what's in that text. It's probably a case of escaping your quotes. show us what's on that line.
     
    sarahk, May 14, 2006 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If the JavaScript is non-dynamic (as in, it doesn't change, or at least doesn't change much), the easiest thing to do is to break out of your PHP and just write the JavaScript directly as if it were in HTML...

    That is to say, use:

    ?>

    to stop the PHP parser then write your JavaScript as if it were in an HTML page then use:

    <?php

    to start the PHP parser up again...
     
    TwistMyArm, May 14, 2006 IP
    zdestiny likes this.