Introduction
In this article I explain how to use various types of popup boxes in PHP. If you learn JavaScript then you know that JavaScript has the following three types of popup boxes:
- Alert box
- Confirm box
- Prompt box
Now here I explain all these popup boxes with PHP one by one.
Alert Box
An alert box is used if you ensure the information comes through the user. It means an alert box pop-up, when you click on a "button".
Syntax
alert("Write some thing here"); |
Example
An example of the alert box is:
<?php
echo '<script type="text/javascript">
window.onload = function () { alert("Welcome at c-sharpcorner.com."); }
</script>';
?>
Output
Confirm Box
A confirm box is used when you want the user to verify or accept something.
Syntax
confirm("Write some thing here"); |
Example
An example of the confirm box is,
if the user clicks "ok" then the confirm box returns true and is the user clicks "Cancel", then the box returns false.
<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
When the "Click Me" Button is pressed:
After pressing the "Ok" button:
After pressing the "Cancel" button:
Prompt Box
A prompt box is used when you want the user to input a value before entering a page and when a prompt box pops up, the user will need to click either "OK" or "Cancel" to proceed after entering an input value.
Syntax
prompt("Write some thing here"); |
Example
An example of the prompt box is:
<html>
<head>
<script>
function myFunction() {
var x;
var site = prompt("Please enter Something", "Write Here Something");
if (site != null) {
x = "Welocme 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
When the "Prompt Box" button is pressed:
After writing the name:
After pressing the "Ok" button: