简体   繁体   中英

php Javascript variable quoting error

I'm having trouble with quotes in a line of the attached code.

It is part of a picture viewer. Picture data url "PicNotes" is read from mysql. I am attempting to enhance the result by adding a picture info pop-up but can't get the quotes right.

I have added some rem statements around 3 versions (attempts) to get it working.

    $data = mysql_query("SELECT * FROM $tbl_name WHERE type='$type' LIMIT $start, $limit_col_1");}
// Your while loop here
while($row = mysql_fetch_array($data))
{//REM If there is no info don't show the info link
    if ($row[PicNotes]) {
//  $icon=<a href="JavaScript:Popup('notes/$row['PicNotes']');"> <img src='images/info.png'></a>; //REM This line was the original call for the pop-up script
//  $icon = "<a href=notes/tempest_series.php><img src=images/info.png></a>"; //REM This line works but does not have any of the Jarvascript or URL variable from the DB
//  $icon = "<a href=notes/$row[PicNotes]><img src=images/info.png></a>"; //REM This  line doesn't crash but the URL is corrupted
    $icon = "<a href="JavaScript:Popup(notes/$row[PicNotes]);"><img src=images/info.png></a>"; //REM This line crashes with an "Unexpected T_STRING error 
    }else{
    $icon='';}
// Display LHS Thumbnail and Viewer Pic Data

    echo "<a href='images/".$row['vfile']."' rel='enlargeimage::mouseover' rev='loadarea' title='&lt;b&gt;".$row['Title']."&lt;/b&gt;&lt;br /&gt;".$row['Medium']." &nbsp; &nbsp; &nbsp; ".$row['iw']." x ".$row['ih']." cm. $icon'><img border='1' src='images/".$row['tnfile']."
    ' alt='' width='".$row['tnx']."' height='".$row['tny']."' class='tn' /></a><br />";

}

Please can somebody put me on the right track.

1) Escape the inner quotes:

$icon = "<a href=\"JavaScript:Popup(notes/$row[PicNotes])\"><img src=images/info.png></a>";

2) Use single quotes:

$icon = "<a href='JavaScript:Popup(notes/$row[PicNotes])'><img src=images/info.png></a>";

使用花括号({})进行更好的变量连接

$icon="<a href=\"JavaScript:Popup('notes/{$row[PicNotes]}');\"><img src=\"images/info.png\"></a>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM