PHP & Basic JavaScript breaking each other

Discussion in 'PHP' started by crazyryan, Jan 18, 2008.

  1. #1
    Hey

    echo '<li><a href="/game/' . $row['id'] .'/" onMouseover="ddrivetip(\'<img src=/' . $row['thumbnail'] . ' /><strong>test</strong>\',\'black\')";
    onMouseout="hideddrivetip()">' . $row['title'] . '</a></li>';
    PHP:
    I've got a tooltip when I hover a link and the PHP and JS are conflicting.

    At the moment I can't use any "s within the tooltip, which means I can style the tooltip contents properly. I want to put

    <img src="/' . $row['thumbnail'] . '" />' . $row['description'] . '

    In the tooltip but it won't let me, I know why but I just can't see a work around because I don't usually work with JS.

    Thanks :confused:
     
    crazyryan, Jan 18, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Rewrite your code for easy of use and clarity:

    
    echo <<<eof
    <li><a href="/game/{$row['id']}/" onMouseover="ddrivetip('<img src=\\'/{$row['thumbnail']}\\' /><strong>test</strong>','black');" onMouseout="hideddrivetip()">{$row['title']}</a></li>
    eof;
    
    PHP:
    Try something on the lines of that.

    Jay
     
    jayshah, Jan 18, 2008 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Thanks, that kind of works.

    I edited the code to this:
    echo <<<eof
    <li><a href="/game/{$row['id']}/" onMouseover="ddrivetip('<img src=\\'/{$row['thumbnail']}\\' style=\'float:left; border: 1px solid #fff; margin: 6px;\' />{$row['description']}','black', 150);" onMouseout="hideddrivetip()">{$row['title']}</a></li>
    eof;
    PHP:
    However, now the tooltips only show for some files...?
     
    crazyryan, Jan 18, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Does the description contain an apostrophe (')? That would break it.

    Jay
     
    jayshah, Jan 18, 2008 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    Oh, yeah, it does.. Can I use str_replace or something?

    EDIT:
    if(strpos($row['description'], "'")) {
    								$row['description'] = str_replace("'", "", $row['description']);
    							}
    PHP:
    Seems to work, but will anything other than a ' break it?
     
    crazyryan, Jan 18, 2008 IP
  6. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    try using the HTML codes for special characters in your descriptions, like:

    & #39; for ' (without the space - it shows up as ' if i try to use the code without the space between the &)
    &quot; for "

    etc.


    That should keep it from breaking.

    More info on special HTML entities here: http://htmlhelp.com/reference/html40/entities/special.html
     
    james_r, Jan 19, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Just use addslashes();

    
    $row['description'] = addslashes($row['description']);
    
    PHP:
    Jay
     
    jayshah, Jan 19, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Or addcslashes(), to leave double quotes as they are (They don't need to be escaped).
    
    $row['description'] = addcslashes($row['description'], "'");
    
    PHP:
     
    nico_swd, Jan 20, 2008 IP
  9. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I prefer a combination of HTML entities and escaping input, i guess it all depends what you like :)
     
    james_r, Jan 20, 2008 IP
  10. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #10
    I agree with james. i think compination with HTML will make it lot easier.
    addslash() is new to me. thanks for this.
     
    Sabbir, Jan 20, 2008 IP