2012-08-08 39 views
-3

我一直在使用此代码从数据库中删除数据。我喜欢的是,无论何时用户单击图像链接以删除确认功能中的数据,都会提示并要求采取措施,我在此代码中遇到了错误。php使用javascript验证函数的超链接

$delurl = "delete_dish.php?dish_id=".$row['id']; 
$img = "<img src = 'images/delete.png'>"; 
echo "<a href=".$delurl."; 
echo "onclick='return confirm('Are you sure you want to delete.')'>".$img."</a>"; 

也许错误是在双引号或单引号,任何帮助提前

感谢

+2

什么是错误? – Don 2012-08-08 17:50:49

回答

-1

你可以,可以而且应该处理的东西时,这样的逃生:

echo "<a href=\".$delurl.\""; 
echo " onclick=\"return confirm('Are you sure you want to delete.')\">".$img."</a>"; 

lg,

flo

1

变化

echo "<a href=".$delurl."; 

echo "<a href=\"".$delurl."\" "; 
0

我想我们以下,而不是逃避,这是更可读的对我说:

$delurl = "delete_dish.php?dish_id=".$row['id']; 
$img = "<img src = 'images/delete.png'>"; 
?> 
<a href="<?=$delurl?>" onclick="return confirm('Are you sure you want to delete?')"><?=$img?></a> 
1
$delurl = "delete_dish.php?dish_id=".$row['id']; 
$img = "<img src = 'images/delete.png'>"; 

$confirm_box <<<CONFIRM 
<a href="$delurl" 
onclick="return confirm('Are you sure you want to delete?')">$img</a> 
CONFIRM; 

// then elsewhere ... 
echo $confirm_box 

始终朝着使用趋向HEREDOC syntax来构建HTML/JS输出,它会为你节省很多心痛。只要注意主要问题,不要忘记heredoc声明的第一行/最后一行。

编辑好处是您可以尽可能多地混合使用单引号和双引号,您只需要担心JS引用 - PHP变量插入时不带引号。您可以进一步在您的PHP变量(如{$ this})中包含大量引号以便更易于阅读,也可以描述$ this和{$ this}。