2012-08-08 35 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}。