Javascript确认框与PHP

时间:2013-05-16 21:01:01

标签: php javascript

我已经对这个主题做过一些研究,发现在php中使用确认框的方法之一是使用javascript onclick()。我有这个代码不起作用。

 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')中使用单引号时,我也会收到错误。任何想法如何构建这种回声?谢谢

4 个答案:

答案 0 :(得分:2)

你必须逃避单引号,这是你的编辑

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>';

答案 1 :(得分:1)

最小模式是:

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

Live Example | Live Source

请注意, double 引用了JavaScript代码上的属性(onclick)和引号(因为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

答案 2 :(得分: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>';

在这里,我已经逃脱了一组引用,应该为你做的伎俩。

答案 3 :(得分:0)

enter code here 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>';

尝试使用反斜杠转义单引号