Displaying an Alert Message Box in PHP

PHP was started, believe it or not, as an open source project that soon gained considerable popularity as developers and software professionals began discovering its immense use. It was in 1994, that its creator, Rasmus Lerdorf released the first edition of PHP - one of today’s most popular programming languages.

PHP is in essence a server-side, HTML-enabled scripting language. It is most popularly and effectively used to handle databases, dynamic content, session monitoring, and to create full-fledged e-commerce websites.

MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server are only a few of the databases it supports.

What is an Alert in PHP?

A warning message is shown to the user using an alert in PHP.  While PHP does not have the ability to view a warning message box, you can use the JavaScript code embedded within the PHP code to do so. You can use PHP to view a JavaScript warning message box in this manner.

A warning box or alert in PHP is a pop-up window on your computer that displays a message or information that needs the user's attention. Browsers support warning boxes, which are JavaScript dialogue boxes.

PHP is a server-side language, so it does not support pop-up warning messages. The client's browser displays a warning. You need to make JavaScript code in PHP and send it to the browser to send a warning message through PHP. The client-side language is JavaScript.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Types of Pop-up Boxes

Now, have a look at the different types of pop-up boxes that one can make on alert in PHP:

Alert Box

If you want to make sure that the alert in PHP comes from the user, you can use a warning box. When you click on a "tab," a warning box appears.

Example: 

<?php

echo '<script type="text/javascript">

            window.onload = function () { alert("Welcome at c-sharpcorner.com."); }

</script>';

?>

Output

AlertMessageBoxInPHP_1.

Confirm box

When you want the user to affirm or approve something, you use a confirm alert in PHP.

Example

<html>

<head>

<script>

    function myFunction() {

        var x;

        var r = confirm("Press a button!");

        if (r == true) {

            x = "You pressed OK!";

        }

        else {

            x = "You pressed Cancel!";

        }

        document.getElementById("demo").innerHTML = x;

    }

</script>

</head>

<body>

<?php

?>

<button onclick="myFunction()">Click Me</button>

<p id="demo"></p>

</body>

</html>

Output

AlertMessageBoxInPHP_2 

Prompt Box

When there is a prompt alert in PHP, the user must press "OK" or "Cancel" to continue after entering an input value.

Example

<html>

<head>

<script>

    function myFunction() {

        var x;

        var site = prompt("Please enter Something", "Write Here Something");

        if (site != null) {

            x = "Welcome at " + site + "! Have a good day";

            document.getElementById("demo").innerHTML = x;

        }

    }

</script>

</head>

<body>

<?php

?>

<button onclick="myFunction()">Prompt Box</button>

<p id="demo"></p>

</body>

</html>

Output

AlertMessageBoxInPHP_3

Add Another Star to Your Performance Evaluation

Learn from industry experts for FREEStart Learning
Add Another Star to Your Performance Evaluation

How to Display an Alert Box in PHP? 

An alert box or alert in PHP is used on the website to notify the user that they have entered a value that is different from what is needed to fill in that role. For friendlier posts, a warning box may still be used. To pick and continue, the warning box only has one button, “OK.”

The warning message appears on the computer as a pop-up display. You will use this to provide the user with information and a message. Since PHP is a server-side language, it does not support warning message boxes, but you can use the JavaScript code inside the PHP body to alert the message box on the computer.

Example:

<?php

// PHP program to pop an alert

// message box on the screen 

// Display the alert box 

echo '<script>alert("Welcome to Geeks for Geeks")</script>';

?>

AlertMessageBoxInPHP_4

Example 2: 

<?php

// PHP program to pop an alert

// message box on the screen

// Function defnition

function function_alert($message) {   

    // Display the alert box 

    echo "<script>alert('$message');</script>";

// Function call

function_alert("Welcome to Geeks for Geeks"); 

?>

Output

AlertMessageBoxInPHP_5. 

Looking forward to becoming a PHP Developer? Then get certified with the PHP Training Course. Enroll now!

Conclusion

Displaying an alert box in PHP is easy for someone well versed in coding. To learn how you can display the right kinds of pop-up boxes you need to know everything about the language. Enroll yourself in the right course from Simplilearn and you can enjoy endless opportunities. 

The alerts in PHP courses are designed so that the developers can explore their skills and give out the best results even on complex problems. Businesses these days require a lot of things to give shape to their website and hence our skill up courses will help you to polish your expertise and get placed in the right organization.

Have any questions for us on this “alert in php” tutorial article? If yes, we’d request you to share them with us by messaging us in the comments section of this article, below. Our experts will get to them and resolve them for you ASAP.

Happy learning! 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.