-1

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?

6
  • 2
    "Doesn't work" is a horrible description of any problem. Instead, describe exactly what the problem is - in this case, what is the output and how does it differ from what you expect? Commented Feb 27, 2014 at 8:35
  • Can you show the output of echo "$phpVariable"? Commented Feb 27, 2014 at 8:35
  • You dont need to add backslashes in above code and what you are trying is possible, You need tomention errors you enconter in tool like firebug , so someone can help actually Commented Feb 27, 2014 at 8:35
  • @Ic the code I given above is simple to follow. It will alert 'passed' when satisfied. So the word "doesn't work" is no need to elaborate. When it doesn't work, the alert 'passed' is not showing even if the event is fired. Commented Feb 27, 2014 at 8:41
  • 1
    @IvorySantos So you're telling me you've looked at the source of the resulting page and it looks exactly the same; that the only difference between when hardcoded and when using a variable is that the alert never shows? Commented Feb 27, 2014 at 9:10

3 Answers 3

0
$phpVariable =  '<script>
$(document).ready(function(){
   $(\'.delete\').click(function()  
    {
        alert(\'passed\');
    });
});
</script>';


echo $phpVariable;

Basically change all " into ' and then escape any ' within the code.

Sign up to request clarification or add additional context in comments.

2 Comments

How does that change anything?
using " instead of ' evaluates all dollar signs within the code as variables if they arent escaped. Escaping apostrophe instead of dollar signs is the more sensible approach. I believe escaping the dollar sign is probably the issue.
0

You can not view Javascript in front end..

View source code of page, It already printed...

try this ...

echo htmlspecialchars($phpVariable);

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.