134

How to pop an alert message box using PHP?

Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
adil
  • 1,387
  • 2
  • 9
  • 4

9 Answers9

295

You could use Javascript:

// This is in the PHP file and sends a Javascript alert to the client
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');</script>";
Peter Gluck
  • 7,914
  • 1
  • 36
  • 35
19

Create function for alert

<?php
alert("Hello World");

function alert($msg) {
    echo "<script type='text/javascript'>alert('$msg');</script>";
}
?>
Deepak Play
  • 331
  • 2
  • 7
3

PHP renders HTML and Javascript to send to the client's browser. PHP is a server-side language. This is what allows it do things like INSERT something into a database on the server.

But an alert is rendered by the browser of the client. You would have to work through javascript to get an alert.

kmoney12
  • 3,657
  • 4
  • 29
  • 51
3

I have done it this way:

<?php 
$PHPtext = "Your PHP alert!";
?>

var JavaScriptAlert = <?php echo json_encode($PHPtext); ?>;
alert(JavaScriptAlert); // Your PHP alert!
Riccardo Volpe
  • 1,145
  • 1
  • 11
  • 25
0

Use jQuery before the php command alert

xKobalt
  • 1,478
  • 2
  • 11
  • 19
0

See this example :

<?php
echo "<div id='div1'>text</div>"
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery1.3.2/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#div1').click(function () {
                alert('I clicked');
            });
        });
</script>
</head>
<body>

</body>
</html>
mrbengi
  • 3,184
  • 11
  • 45
  • 79
0

This .php file content will generate valid html with alert (you can even remove <?php...?>)

<!DOCTYPE html><html><title>p</title><body onload="alert('<?php echo 'Hi' ?>')">
Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
0

You need some JS to achieve this by simply adding alert('Your message') within your PHP code.

See example below

     <?php 

//my other php code here
        
        function function_alert() { 
              
            // Display the alert box; note the Js tags within echo, it performs the magic
            echo "<script>alert('Your message Here');</script>"; 
        } 
        
        ?> 

when you visit your browser using the route supposed to triger your function_alert, you will see the alert box with your message displayed on your screen.

Read more at https://www.geeksforgeeks.org/how-to-pop-an-alert-message-box-using-php/

B'Okello
  • 75
  • 10
-4

You can use DHP to do this. It is absolutely simple and it is fast than script. Just write alert('something'); It is not programing language it is something like a lit bit jquery. You need require dhp.php in the top and in the bottom require dhpjs.php. For now it is not open source but when it is you can use it. It is our programing language ;)

dur
  • 13,039
  • 20
  • 66
  • 96
Ibrahim Hasanov
  • 393
  • 1
  • 3
  • 10
  • 2
    if you announce some unknown php library, at least link to it. Also, it most likely is *based* on some programming language like Javascript, seems misleading... – IceFire Mar 26 '16 at 10:41
  • I know but I think when it was finished this will be useful – Ibrahim Hasanov Mar 26 '16 at 10:56
  • Yeah already been said, but this does not seem like a usable solution... alert('something'); is JS. I can write a PHP function called 'alert' that creates a JS alert like another post has already mentioned. This does not seem like a useful external library at all, just some random PHP functions. I'm not saying whatever you are creating is bad because I have no clue what it is, but the way you describe it as a solution is. – ViaTech Feb 12 '18 at 19:59