Ok, what I’m doing right now is just echoing an script tag. For example:
echo '<script>alert("Hello world")</script>';
Is this the right way to do this? or is there a better way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You cannot execute JavaScript from PHP. Unless you are using Server-side JavaScript, all JavaScript is only ever executed on the client side – in the browser. That means your PHP code only writes the JS code onto the page the browser will show later. This page is served from your webserver to the requesting browser and that is where and when the JS code is executed then. For PHP, the JS you echo has no meaning. It is just a string like everything else.
Apart from this, yes. This is how you can do it.