2013-05-16 43 views
0

我已經做了一些關於這個問題的研究,發現在php中使用確認框的方式之一是與JavaScript onclick()。我有這個代碼不起作用。Javascript確認框與PHP

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'" style="color:red" onclick="return confirm("Are you sure you want to delete this product ?")">Delete</a></td>'; 

我認爲這個問題是與'"使用,但我不知道如何構建這個回聲。當我在confirm('Are you sure you want to delete this product')中使用單引號時,我也遇到了錯誤。任何想法如何我可以構造這個回聲?感謝

回答

2

必須轉義單qoute,這裏是你的編輯

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'" style="color:red" onclick="return confirm(\'Are you sure you want to delete this product ?\')">Delete</a></td>'; 
+0

感謝,這是它.. – dhani

1

最小MOD是這樣的:

onclick="return confirm('Are you sure you want to delete this product ?')" 

Live Example | Live Source

請注意,這是在屬性(onclick雙引號,並在JavaScript代碼引號(因爲JavaScript的支持使用單引號的字符串)。

如果您需要在消息中包含撇號(這很常見),請記住屬性的內容是HTML文本,並且在HTML文本中您可以使用HTML實體。所以這個作品也是:

onclick="return confirm(&quot;You're really sure want to delete this product ?&quot;)" 

Live Copy | Live Source(我改變了消息,所以它包括一個'

雖然在這種情況下的另一種選擇是使用一個轉義撇號:

onclick="return confirm('You\'re really sure want to delete this product ?')" 

Live Copy | Live Source

0

您只需要小心使用引號:PHP String Reference

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'" style="color:red" onclick="return confirm(\'Are you sure you want to delete this product ?\')">Delete</a></td>'; 

在這裏,我已經逃過了一套應該爲你做詭計的引號。

0
enter code here echo '<td class="item_unsold"><a href = "manage-products.php?prod='[email protected]$row[0].'" style="color:red" onclick="return confirm(\'Are you sure you want to delete this product ?\')">Delete</a></td>'; 

嘗試逃避單引號難熬反斜線