Online Tutorials & Training Materials | STechies.com
Register Login

How to Display Alert Message Box in PHP?

|| 0

How to Display Alert Message Box in PHP?
Stechies

JavaScript has three types of pop-up boxes, which are the following:

  • Alert box
  • Confirm box
  • Prompt box

In this article, you will learn about the alert box, confirmation box, and prompt box with example.

Alert box

An alert box is nothing but a pop-up window box on your screen with some message or information which require user attention. 

An alert box is a JavaScript dialog box which is supported by browsers.

PHP is a server-side language and does not support pop-up alert messages. An alert is rendered by the browser of the client.

In order to pop an alert message via PHP, we need to render JavaScript code in PHP and send it to the browser. JavaScript is a client-side language.

Syntax:

alert("Type your message here");

Example: Using JavaScript alert box

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Alert Box by PHP</title>
<?php  
echo '<script type="text/javascript">';
echo ' alert("JavaScript Alert Box by PHP")';  //not showing an alert box.
echo '</script>';
?>
</head>

<body>
</body>
</html>

Output:

<script Type="javascript">alert("JavaScript Alert Box by PHP")</script>

Example: Using PHP function

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Alert Box by PHP</title>
<?php
function_alert("We welcome the New World");

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

Output:

<script type='text/javascript'>alert('We welcome the New World');</script>

Confirm box

A confirm box mostly used to take user approval to verify or accept something.

Syntax:

confirm("Type your message here");

Example: Using Javascript confirmation pop-up box

<?php
function  createConfirmationmbox(){
    echo '<script type="text/javascript"> ';
    echo ' function openulr(newurl) {';
    echo '  if (confirm("Are you sure you want to open new URL")) {';
    echo '    document.location = newurl;';
    echo '  }';
    echo '}';
    echo '</script>';
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Alert Box by PHP</title>
<?php
createConfirmationmbox();
?>
</head>
<body>
<strong><a href="javascript:openulr('newurl.html');">Open new URL</a></strong>
</body>
</html>

Output:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Alert Box by PHP</title>
<script type="text/javascript">  function openulr(newurl) {  if (confirm("Are you sure you want to open new URL")) {    document.location = newurl;  }}</script></head>
<body>
<strong><a href="javascript:openulr('newurl.html');">Open new URL</a></strong>
</body>
</html>

Prompt box

A prompt box is mostly used, when you want the user input, the user needs to fill data into the given field displaying in the pop-up box and has to click either ok or cancel to proceed further.

Syntax:

prompt("Type your message here");

Example: Using Javascript prompt pop-up box

<?php
function  createConfirmationmbox(){
    echo '<script type="text/javascript"> ';
    echo 'var inputname = prompt("Please enter your name", "");';
    echo 'alert(inputname);';
    echo '</script>';
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Prompt Box by PHP</title>
<?php
    createConfirmationmbox();
?>
</head>
<body>
</body>
</html>

Output:

<script type="text/javascript"> var inputname = prompt("Please enter your name", "");alert(inputname);</script></head>