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

    Join Date
    May 2001
    Posts
    25
    Rep Power
    0

    Problem with alert: javascript is printed by php


    Here's a code sample:
    PHP Code:
             $sql "SELECT * FROM classes WHERE teacher = '$tid' ORDER BY period ASC";
             
    $sql_result mysql_query($sql);
             while (
    $row mysql_fetch_array($sql_result)) {
                 
    $cid $row[cid];
                 
    $period $row[period];
                 
    $grades $row[grades];
                 
    $description eregi_replace('"'"'",  $row[description]);
                 
    $class_name addslashes($row[class_name]);
                 echo 
    "<td><center>"; if($description) { echo "<a href=\"javascript:alert('$description');\">";} echo $class_name; if($description) { echo "</a>";}if($grades){echo " (".$grades.")";} echo "<br><input type=radio name=period_$period value=\"$cid\"></center></td>\n";
              } 
    When clicked on, the link does nothing. I want it to work even if " or ' are in the description.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    New York
    Posts
    122
    Rep Power
    14
    If it's quotes that are causing your problems, you're going to have to escape them with backslashes or replace them in $description via php before concating the string you're echoing.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2001
    Location
    USA
    Posts
    830
    Rep Power
    14
    hi thedanmichael,

    I think I see a couple of problems with your code:

    Where you have the link, it looks like you've got 'java script'. It should be 'javascript' -- no space.


    This part
    Code:
    echo "<a href=\"java script:alert('$description');\">";
    I think what you're trying to do is insert the value of PHP variable 'description' in the JavaScript alert(), in which case I believe you need to concatenate the strings you're echoing, like this

    Code:
    echo "<a href=\"javascript:alert('", $description, "');\">";
    or this

    Code:
    echo "<a href=\"javascript:alert('" . $description . "');\">";
    When I use the PHP echo statement with HTML, I use single quotes for the echo statement because with HTML you're almost always using double quotes (of course you frequently use single quotes in JS, but...)

    I hope this helps

IMN logo majestic logo threadwatch logo seochat tools logo