简体   繁体   中英

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() . 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

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

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).

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. 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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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