Possible Duplicate:
How to call a JavaScript function from PHP?
Can I add a javascript alert inside a PHP function? If yes, how?
Yes, you can, though I 100% guarantee this isn't what you want or what you mean:
<?php
function do_alert($msg)
{
echo '<script type="text/javascript">alert("' . $msg . '"); </script>';
}
?>
<html><head><title>Hello</title></head>
<body>
<h1>Hello World, THis is my page</h1>
<?php
do_alert("Hello");
?>
</body>
</html>
The browser runs the Javascript, the server runs the PHP.
You could also echo Javascript from your server (without HTML) and then include that script into your page by dynamically creating a <script>
tag containing that Javascript. This is essentially creating Javascript on the fly for injection into your page with the right headers etc.
If you want to trace some PHP script execution, then you can use trigger_error()
to create a log entry, or you could write a trace()
function to store strings in a buffer and add them to a page.
If you want to trace Javascript, See Firebug for Firefox.
PHP Headers API Documentation
On-demand Javascript at Ajax Patterns
Yes, you can.
echo "<script>alert (\"js inside php\")</script>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With