I have a javascript that works fine
<script>
$(document).ready(function(){
$('.delete').click(function()
{
alert('passed');
});
});
</script>
But when I saved all those script to a PHP variable
$phpVariable = "<script>
\$(document).ready(function(){
\$('.delete').click(function()
{
alert('passed');
});
});
</script>";
and echoed the variable (with or without backslash on dollar '$' sign inside javascript)
echo "$phpVariable";
The problem exists. Javascript doesn't work anymore. Can we save javascript code to php variable without encountering mulfunction?
3 Answers 3
$phpVariable = '<script>
$(document).ready(function(){
$(\'.delete\').click(function()
{
alert(\'passed\');
});
});
</script>';
echo $phpVariable;
Basically change all " into ' and then escape any ' within the code.
2 Comments
You can not view Javascript in front end..
View source code of page, It already printed...
try this ...
echo htmlspecialchars($phpVariable);
Comments
The right answer is here
adding only
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
above the place where you are going to echo the $phpVariableScript
Sorry for some kind of duplicate question. I found the right answer after I posted the question. Should I delete this or link to the right answer?
echo "$phpVariable"?