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.

$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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Learn how to add Scroll Indicator using CSS and JavaScript?CSSHow to Make a Simple Module with a Form and Menu Link in Drupal 7.x?DrupalArbitrary Arguments in SCSS functionsSCSSURL paths in DrupalDrupalA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscellaneousHow to create a flip pricing table using CSS and JavaScript?CSS