#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    3
    Rep Power
    0

    Question javascript button in PHP


    I'm trying to insert the following code (orginally in a html file) into a php script. The php script already has html in ans that's working.

    the following was added to the <HEAD></HEAD> section:

    <script language="JavaScript" src="script.js"></script>

    the following was added to a existing form in the html section:

    <input type=text name="datebox" size=15><a href="javascript:show_calendar('calform.datebox');" onmouseover="window.status='Date Picker';return true;" onmouseout="window.status='';return true;"><img src="pic.gif" width=24 height=22 border=0></a>

    however it complains about the javascript call in the <a href></a> section. Any ideas how this can be accomplished?

    oh yeah, I'm not closing my php tags to display html...just echoing the html out of the php.

    Thanks in advance,
    -Techniq
  2. #2
  3. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    How are you echoing out the HTML? If you're not back-escaping your quotes, you'll cause problems.

    PHP Code:
    echo "<a href="somewhere.html">"// will break
    echo "<a href="\somewhere.html\">"// correct
    echo '<a href="somewhere.html">'// also correct 

    Hope this helps.
    Alex
    (http://www.alex-greg.com)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    3
    Rep Power
    0
    I'm pretty sure I;m echoing correctly b/c I'm adding this to a html section that is already being echoed out w/o a problem.

    Here's a sample of how I'm echoing out the html:

    PHP Code:
    <?php

    echo'
    <HTML>
    <HEAD>
    <script language="JavaScript"></script>
    </HEAD>
    <BODY>
    <input type=text name="datebox" size=15>
    <a href="javascript:show_calendar('
    calform.datebox');"
    onmouseover="window.status='
    Date Picker';return true;"
    onmouseout="window.status='';return true;">
    <img src="pic.gif" width=24 height=22 border=0></a>
    </BODY>
    </HTML>'
    ;

    ?>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Sweden
    Posts
    32
    Rep Power
    15
    Your problem is infact in how you ehco the html.
    I saw that you use echo' with a ' to enclose the html string (supposedly to not having to escape your "). Now, take a look at the javascript function call:
    show_calendar('calform.datebox') there you go, you terminate the echo statement with those ' in the function call.

    Imho you are better off to properly escape your " in the future, like this \". That way you won't get into trouble this way.
    Plus, if you use print(" "); at some time (don't know about echo) you will notice that it treats php variables in the string differently if you use ' instead of " to enclose the string.

    To properly escape the critical chars is simply good programming practice

    //Dan
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    London
    Posts
    11
    Rep Power
    0
    If you really want to write a big lump of text that way, use heredoc syntax. e.g.:

    PHP Code:
    echo <<<EOF
    Hello world<BR>
    "This is quoted"<BR>
    This isn't
    EOF; 
    Then it doesn't matter about quoting.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    3
    Rep Power
    0
    Turns out all I had to do was the escape the single quotes \' . This now works.

    Please let me know if anyone thinks this could cause problems.
  12. #7
  13. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Save yourself a load of hassle and just use the EOF solution. It's the best way of echoing out large blocks of HTML, because you don't have to remember to back-escape quotes each time.
    Alex
    (http://www.alex-greg.com)

IMN logo majestic logo threadwatch logo seochat tools logo