Yogesh Chauhan's Blog

How to Pop an Alert Box in PHP?

in JavaScript & PHP on November 7, 2019

In this small article, I am going to show you guys how to show an alert box in PHP website or a webpage.

Let's get into the code right away.

We can use JavaScript or jQuery to show an alert box in case of showing an success or failure state of an event. For example, if user is submitting some data and it goes through, then we can display this kind of alert boxes to show everything is okay. If user couldn't enter data successfully, we can show failure alert box as well. In other cases, we can just show some general messages like "user profile created" or "password is wrong" etc. etc.


<?php
$alert = "alert box";
echo "<script type='text/javascript'>alert('$alert');</script>";
?>

In the example above, as you see I am just declaring a variable and storing the message I want to display. You can make this variable dynamic and get user input and then display an alert according to the user input as well, it will just require few more lines of code, which I will cover in future articles. 

If you know, JavaScript then it's pretty basic code for you. We are just simply wrapping up the JavaScript code into PHP code. I have used echo statement to execute that JavaScript code. And our alert box is ready!


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Responsive Masonry Grid using CSS columns PropertyCSSHow to enable and disable button using JavaScript?JavaScriptMake sure your links have descriptive textUI/UXWhat are Web services?MiscellaneousClean Form Input With These PHP Functions Before Inserting into DatabasePHPRendering Elements in ReactReact