Results 1 to 4 of 4

Thread: Adding javascript into PHP

  1. #1
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding javascript into PHP

    How to add javascript into PHP

    How would I make this work?

    eg:

    Lets say I have this javascript:

    Code:
    <a href="search.htm" onClick="return overlay(this, 'subcontent2', 'rightbottom')">login</a>
    and I want to add it into this PHP:

    PHP Code:

    <?php

    // Are they a guest?

    if ($context['user']['is_guest'])

    {

     
    // Show a login box...

     // Uncomment below to show login box

     // ssi_login();

     // ... and a little notice about registering

     
    echo '

     Welcome, <b>Guest</b>, Please **THE JAVASCRIPT HERE** or'
    ;

     echo 
    '

     <a href="'
    $scripturl'?action=register">register</a>';

    }

    // Otherwise, a user here :)

    else

    {

     
    // Show them a nice welcome notice

     
    ssi_welcome();

     echo 
    ' ';

     
    // And a logout link

     
    ssi_logout("#");

    }

    ?>

  2. #2
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Don't worry, found a solution:

    Code:
    <a href="#" onclick="return overlay(this, \'subcontent2\', \'rightbottom\')">login</a>
    Just add the backslashes.

  3. #3
    Join Date
    May 2007
    Location
    Sherman Texas
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The problem was in your quotes.
    Both javascript and PHP share the same quotes (single quote ' and the double quote ") so when you place a piece of javascript code into PHP - you need to tell PHP to ignore the javascript quotes that interfer - as Master-T did - by using the \ character.
    Of course - you could also break it up so that the quotes don't interfer with each other - like this:
    Code:
    $link = "return overlay(this, 'subcontent2', 'rightbottom')";
    $link = '<a href="search.htm" onClick="'.$link.'">login</a>';
    I prefer the latter because I find it easier to read - even it it is more work.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    One of many reasons to break out of PHP parsing mode when outputting HTML.
    Code:
    <?php if($context['user']['is_guest']) { ?>
    
    Welcome, <b>Guest</b>.
    Please <a href="search.htm" onclick="return overlay(this, 'subcontent2', 'rightbottom');">login</a>
    or <a href="<?php echo $scripturl; ?>?action=register">register</a>.
    
    <?php } else {
      ssi_welcome();
      ssi_logout('#');
    } ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •