iCoders iCoders - 1 year ago 95
HTML Question

php binding quotes in html

I have following code in php where I am binding html code:

$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(".$base_image_url.$myDoc->my_doc_name.")' value='".$myDoc->my_doc_id."' class='cropcss'/>";

It produces following output:

<img src="mysite.in/8422_1477013411.png" onclick="window.open(mysite.in/8422_1477013411.png)" value="623" class="cropcss">

I have missed single quote inside. How do I add this?

Answer Source

I prefer to use single quotes for outputting variables like HTML to the browser. Simply use \' to escape a single quote, and pass it to the browser. Something like this should work:

$my_doc .= '<img src="mysite.in/' . $myDoc->my_doc_name . '" onclick="window.open(\'' . $base_image_url . $myDoc->my_doc_name . '\')" value="' . $myDoc->my_doc_id . '" class="cropcss" />";

If you insist on using double quotes, a minor quick change can fix this (Just add \" around your text):

$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(\"".$base_image_url.$myDoc->my_doc_name."\")' value='".$myDoc->my_doc_id."' class='cropcss'/>";

For more info, see this post on how to escape quotation marks in php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download