How to pop an alert message box using PHP?

JavascriptPhpMessagebox

Javascript Problem Overview


How to pop an alert message box using PHP?

Javascript Solutions


Solution 1 - Javascript

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>";

Solution 2 - Javascript

Create function for alert

<?php
alert("Hello World");

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

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/

Solution 5 - Javascript

I have done it this way:

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

var JavaScriptAlert = <?php echo json_encode($PHPtext); ?>;
alert(JavaScriptAlert); // Your PHP alert!

Solution 6 - Javascript

Use jQuery before the php command alert

Solution 7 - Javascript

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>

Solution 8 - Javascript

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' ?>')">

Solution 9 - Javascript

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 ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionadilView Question on Stackoverflow
Solution 1 - JavascriptPeter GluckView Answer on Stackoverflow
Solution 2 - JavascriptDeepak PlayView Answer on Stackoverflow
Solution 3 - Javascriptkmoney12View Answer on Stackoverflow
Solution 4 - JavascriptBenson OkelloView Answer on Stackoverflow
Solution 5 - JavascriptRiccardo VolpeView Answer on Stackoverflow
Solution 6 - Javascriptuser1899745View Answer on Stackoverflow
Solution 7 - JavascriptBengi BesçeliView Answer on Stackoverflow
Solution 8 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 9 - JavascriptIbrahim HasanovView Answer on Stackoverflow