简体   繁体   中英

javascript confirm popup within PHP while loop not working

I have the following code setup to delete records from my database. It executes fine except for the javascript confirm message which never pops up. It just gets ignored completely and the record gets deleted. Any ideas why?

 connect_to_db();
    $query="SELECT id, date, title, image FROM content ORDER BY date DESC";
    $result=mysql_query($query);
    $message= "Continue?";
    while($row = mysql_fetch_array($result)){
    echo '<div id="delete" align="center">';
    echo '<a href="delete.php?id='.$row['id'].'" onclick = "if (! confirm('.$message.')) { return false; }" ><img src="'.$row['image'].'" style="border:1px solid black; width:100px;"><br>Delete</a>';
    echo '</div>';
}

Your Continue? message is shown as a bare string in the JavaScript code, which is for obvious reasons invalid.

Try this:

'... onclick="return confirm(&quot;'.$message.'&quot;);" ...'

看起来像是缺少引号的问题:

echo '<a href="delete.php?id='.$row['id'].'" onclick = "if (! confirm(\''.$message.'\')) { return false; }" ><img src="'.$row['image'].'" style="border:1px solid black; width:100px;"><br>Delete</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