9

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

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

Community
  • 1
  • 1
Ahmad Farid
  • 14,398
  • 45
  • 96
  • 136
  • I swear I saw a dupe ... – Aiden Bell Nov 22 '10 at 14:44
  • Oh yea http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – Aiden Bell Nov 22 '10 at 14:44
  • Oh didn't see it! What shall I do now, close it or delete or what? – Ahmad Farid Nov 22 '10 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. – DampeS8N Nov 22 '10 at 15:02

7 Answers7

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

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
  • 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? – Ahmad Farid Nov 22 '10 at 14:33
  • No- the Ajax request will only return the final result and nothing in between (if you're thinking of alerting at different stages) – SW4 Nov 22 '10 at 14:37
  • Javascript can't directly call PHP, Javascript can only invoke PHP via a URL. – Aiden Bell Nov 22 '10 at 14:37
  • I think he's trying to debug his php code with Javascript alerts, if that clears anything up. – Pointy Nov 22 '10 at 14:59
  • Tremendously useful for debugging, cheers! – Aphire Sep 03 '15 at 14:02
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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • That's what I meant yeah. Thanks! – Ahmad Farid Nov 22 '10 at 14:23
  • Then you should select it as the correct answer :) – cgp Nov 22 '10 at 14:32
  • But there were some answers that said it's possible though! Or did I not get it correctly? – Ahmad Farid Nov 22 '10 at 14:49
  • 4
    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. – Pointy Nov 22 '10 at 15:00
6

Yes, you can.

echo "<script>alert (\"js inside php\")</script>";
RobertO
  • 2,655
  • 1
  • 20
  • 18
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>
?>
Rhapsody
  • 6,017
  • 2
  • 31
  • 49
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.

darioo
  • 46,442
  • 10
  • 75
  • 103
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.

mck89
  • 18,918
  • 16
  • 89
  • 106
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>";
}
tommytwoeyes
  • 483
  • 5
  • 19