Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a javascript alert inside a PHP function? If yes, how? [duplicate]

Tags:

javascript

php

Possible Duplicate:
How to call a JavaScript function from PHP?

Can I add a javascript alert inside a PHP function? If yes, how?

like image 506
Ahmad Farid Avatar asked Nov 22 '10 14:11

Ahmad Farid


2 Answers

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

like image 99
Aiden Bell Avatar answered Oct 07 '22 01:10

Aiden Bell


Yes, you can.

echo "<script>alert (\"js inside php\")</script>";
like image 28
atlavis Avatar answered Oct 07 '22 00:10

atlavis