Link 1'; ...">

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


Try the opposite ... use single quotes for html and double quotes for javascript. This is how we really do it.



0


source


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


echo "<a href='#' onclick='updateByQuery(\"Layer3\", \"" . json_encode($query) . "\");'>Link 1</a>";

      

This gives:

<a href='#' onclick='updateByQuery("Layer3", "Ed Hardy");'>Link 1</a>

      

+1


source


echo "<a href='#' onclick='updateByQuery(\"Layer3\", " . json_encode($query) . ");'>Link 1</a>";

      

0


source


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







All Articles