Php quoting problem
I have this PHP code
echo '<a href="#" onclick="updateByQuery(\'Layer3\', ' . json_encode($query) . ');">Link 1</a>';
which generates a link like this:
<a href="#" onclick="updateByQuery('Layer3', "Ed Hardy");">Link 1</a><li>Link 2</li>
The javascript call will not be called. How can I get it to generate single quotes around the $ query result, in this case ed hardy?
0
source to share
5 answers
You have to html encode it:
echo '<a href="#" onclick="updateByQuery(\'Layer3\', ' . htmlentities(json_encode($query)) . ');">Link 1</a>';
You can also use htmlspecialchars
+2
source to share
Quotes are a problem with inline handlers. As RoBerg says, you need to use htmlentities in the text.
Another way is to use hook methods and anonymous functions rather than inline handlers.
echo '
<a href="#" id="link_1">Link 1</a>
<script>document.getElementById("link_1").onclick =
function() { updateByQuery("Layer3", '.json_encode($query).'); }
</script>
';
0
source to share