简体   繁体   English

Java脚本确认框与PHP

[英]Javascript confirm box with php

i've done some research on this subject and found that one of the ways to use a confirm box in php is with the javascript onclick() . 我对此主题进行了一些研究,发现在php中使用确认框的一种方法是使用javascript onclick() I have this code that doesn't work. 我有此代码不起作用。

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

I think the problem is with the usage of ' and " but i'm not sure how to structure this echo. When i use single quotes in confirm('Are you sure you want to delete this product') i get an error as well. Any ideas how i can structure this echo? thanks 我认为问题出在'"的使用上,但是我不确定如何构造此回显。当我在单引号中使用confirm('Are you sure you want to delete this product')我也会收到错误消息我有什么想法可以构造这种回声吗?

您必须转义单个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>';

The minimal mod is this: 最小的mod是这样的:

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

Live Example | 现场示例 | Live Source 现场直播

Note that that's double quotes on the attribute ( onclick ), and single quotes on the JavaScript code (since JavaScript supports using single quotes for strings). 请注意,这是属性( onclick )上的引号,以及JavaScript代码上的引号(因为JavaScript支持对字符串使用单引号)。

If you needed to include an apostrophe in your message (which is fairly common), remember that the content of an attribute is HTML text, and in HTML text you can use HTML entities. 如果需要在消息中包含撇号(这是很常见的),请记住,属性的内容是HTML文本,并且在HTML文本中可以使用HTML实体。 So this works too: 所以这也可行:

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

Live Copy | 实时复制 | Live Source (I changed the message so it included an ' .) 实时源 (我更改了消息,因此其中包含一个' 。)

Although the other option in that situation is to use an escaped apostrophe: 尽管在这种情况下,另一种选择是使用转义的撇号:

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

Live Copy | 实时复制 | Live Source 现场直播

You just need to be careful of your use of quotes: PHP String Reference . 您只需要小心使用引号即可: 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>';

Here I have escaped a set of quotes that should do the trick for you. 在这里,我逃避了一组引号,这些引号应该可以为您解决问题。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM