admin管理员组文章数量:1406032
How can I bine this code with all single and double quotes as it should be. I have tried several binations and I can't make it work. This one is my last try so please help.
What would be a good approach when working with long strings?
$html .='<a href="" onClick="$.ajax({type: "POST",url: "delete_pic.php",data:{id:"'.$row['id'].'",var:"V"},cache: false});" style="background:url("images/icons/delete.png" 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';
How can I bine this code with all single and double quotes as it should be. I have tried several binations and I can't make it work. This one is my last try so please help.
What would be a good approach when working with long strings?
$html .='<a href="" onClick="$.ajax({type: "POST",url: "delete_pic.php",data:{id:"'.$row['id'].'",var:"V"},cache: false});" style="background:url("images/icons/delete.png" 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';
Share
Improve this question
asked Apr 24, 2013 at 16:39
user123_456user123_456
5,82526 gold badges87 silver badges142 bronze badges
2
-
I think the problem is in the
style
attribute part. The opening quotation for the url of the background closes the opening quotation of thestyle
attribute. Maybe you should follow @Jeffrey's answer below, and separate your stylesheet rather than make it inline :) – Arnelle Balane Commented Apr 24, 2013 at 16:44 - 1 2 notes here: you are missing the closing ) on the url(, and as strange as it seems, you dont need any quotes around the url – Uberfuzzy Commented Apr 24, 2013 at 16:46
4 Answers
Reset to default 3I would move your styles to an external stylesheet to make it shorter, and then just escape the quotes like "\"" for " in the string.
$html .="<a href=\"\" onClick=\"$.ajax({type: \"POST\",url: \"delete_pic.php\",data:{id:\".$row["\id\"].",var:\"V\"},cache: false});\" class=\"mystyle\"></a>";
This was not tested because I don't have your code :)
Best solution is to use HEREDOC, which pletely eliminates the need for ANY quote escaping at the PHP level:
$html .= <<<EOL
<a href="onclick('\$.ajax({ etc.....
EOL;
Note that you'll still be bound by the quoting needs of whatever language(s) you're embedding in the heredoc. But at least you won't have to worry about causing a PHP syntax error because of unbalanced/unescaped quotes.
I follow the rule of: php strings are encapsulated in single quote, so attributes of html are in double quotes.
Any quote in the attribute must be an escaped single quote \'
so:
$html .='<a href="" onClick="$.ajax({type: \'POST\',url: \'delete_pic.php\',data:{id:\''.$row['id'].'\',var:\'V\'},cache: false});" style="background:url(\'images/icons/delete.png\' 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';
You should probably just escape the double-quotes inside the other double-quotes (if that makes sense). :)
$html .='<a href="" onClick="$.ajax({type: \"POST\",url: \"delete_pic.php\",data:{id:\"'.$row['id'].'\",var:\"V\"},cache: false});" style="background:url(\"images/icons/delete.png\" 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';
That (or something similar) should work.
本文标签: javascriptsingle and double quotes in php variableStack Overflow
版权声明:本文标题:javascript - single and double quotes in php variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744966718a2634992.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论