10

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

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

4
  • I swear I saw a dupe ... Commented Nov 22, 2010 at 14:44
  • Oh yea stackoverflow.com/questions/1045845/… Commented Nov 22, 2010 at 14:44
  • Oh didn't see it! What shall I do now, close it or delete or what? Commented Nov 22, 2010 at 14:49
  • Additionally, based on the chatter around this post, what you REALLY want is to use firefox with the firebug plugin. Then, you open firebug on this page in question and make sure the Console is enabled. Then, you add echos to your PHP. When the AJAX is called, a little doodad will appear in the console, and there you can view the response, which will contain all your echo'd info. Commented Nov 22, 2010 at 15:02

7 Answers 7

22

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

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

5 Comments

There is some javascript code that calls other PHP code in another file. I want to trace this PHP code and see it while running. This is what I want it for. Does this idea sound correct?
No- the Ajax request will only return the final result and nothing in between (if you're thinking of alerting at different stages)
Javascript can't directly call PHP, Javascript can only invoke PHP via a URL.
I think he's trying to debug his php code with Javascript alerts, if that clears anything up.
Tremendously useful for debugging, cheers!
12

Assuming that what you mean is that you want an alert to pop up from the browser when the server reaches a certain point in your php code, the answer is "no".

While the server is running your php code, there's no contact with the browser (until the response starts coming back). There are situations in which a server may be streaming content back to the browser, and in such a case it'd be possible to have some agreed-upon convention for the server to mark the content stream with messages to be alerted out immediately, but I don't think that's what you're asking about here.

4 Comments

That's what I meant yeah. Thanks!
Then you should select it as the correct answer :)
But there were some answers that said it's possible though! Or did I not get it correctly?
It is possible for the php code to produce an HTML page that, when it reaches the browser and the browser has interpreted it, shows an alert. If you're trying to have an alert happen while the server is in the middle of processing your php code, then that is impossible. If it's OK to wait until the server-side php code is completely finished before you see the "alert", then you can do it.
6

Yes, you can.

echo "<script>alert (\"js inside php\")</script>";

1 Comment

uh oh! no one asked for more context or explanation. Bad comment :D
3

To explain; PHP is compiled at the server and the result is plain HTML. Therefore alerts and such cannot appear while compiling is a silent process.

If you want to show an alert in the HTML generated by PHP;

<?php
   echo '<script type="text/javascript"> alert(\'Hi\'); </script>
?>

Comments

0

Yes, if you send Javascript code with alert to a html page from PHP.

No, if you want to call alert just by executing server side code and not sending JS to client browser.

2 Comments

@Ahmad: see Rhapsody's answer
Your response HTML code can contain Javascript code that produces an alert, but you won't see the alert until long after your php code has finished.
0
echo "<script type='text/javascript'>alert('alert text goes here');</script>";

But i don't know how it can be useful because it will work only on the client side. If you want to debug your code you can simply print it on the screen, but if you use the alert take care of quotes because they can break the javascript part.

Comments

0

You can output a Javascript alert() within a PHP function in at least two ways:

A) Return it:

function sendAlert($text) {
    return "<script type='text/javascript'>alert('{$text}');</script>";
}

B: Output it directly:

function echoAlert($text) {
    echo "<script type='text/javascript'>alert('{$text}');</script>";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.