Javascript alert box - display single quotes or apostrophe

I have a problem with displaying of single quote character or apostrophe in javascript alert box. I am using combination of single and double quotes in related PHP / HTML. What should I do to be sure that single quotes will appear in alert box notification ?
0
give a positive ratinggive a negative rating
Hi,

The situation when using mulitple combinations of single and double quotes in PHP is quite common. To show javascript Alert notification with single quote, you will need to use escaped quotes and addslashes() function in your solution.

This combination of regular and escaped single and double quotes should work:

$notification = "'Content in single quotes'";

echo "<a href=\"javascript: void(0);\" onClick=\"javascript: alert('".addslashes($notification)."');\" >Show notification</a>";

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us