How to Display Success & Error Messages in PHP - Coding Cyber

How to Display Success & Error Messages in PHP

Displaying Success & Error Messages in a neat and easy to read manner is the main part in adding PHP Form Validations. After seeing these error messages, the user will finally come to know the information that he/she should submit.

In this tutorial, we will see, how to display success & error messages in PHP. This is a continuation from the previous steps.

==> What You Should Know about Forms in PHP for Developers
==> How to Add PHP Form Validations

Let’s get into displaying success & error messages.

How to Display Error Messages in PHP

In the previous step, we have already added the PHP Form Validations. Based on that validations, I’m going to assign those error messages into variables or array. Messages assigned in these variables or arrays will be displayed on the PHP page.

Displaying Errors after Input Fields

For displaying Errors after Input Fields, you have to assign these error messages to variables. So, these variables can be used to display the error messages, right after the input fields.

And we will create a new variable for every input field while displaying these error messages. We will check whether the input field is set or not empty. If the variable is set, these error messages will be displayed after the input fields.

Adding the Bootstrap Styling to Errors

The difference between success messages and failure messages is the styling. Based on the styling user will understand. For error messages, we will use red color text or red color background. You don’t have to write these styles manually if you are using bootstrap framework in your application. Then you can use alert and alert-danger classes to apply these styles.

Displaying all the Errors in PHP

We have seen displaying individual errors right after the input fields. For displaying all these errors in a single block, we will be using an array to assign these error messages.

Then we will use foreach loop to display all these errors in one place. Mostly, these errors will be displayed above the form.

Before displaying these error messages, we should check the errors array is set and not empty. Then only we will proceed with the foreach loop.

Adding the Bootstrap Styling to Errors

Adding Styling to these error messages is similar to the last step, but instead of adding these bootstrap classes multiple times we will add them once for the foreach loop. And these errors will be displayed in a single block.

Checking if there are Errors to Display

As I’ve already said in the previous step, we should check the errors before using the foreach loop. We can check if there are any errors in errors array with this PHP function empty.

If there are any error messages, then only we will proceed with displaying these error messages.

Displaying Success Messages in PHP

Displaying Success Messages is the same as displaying the error messages, previously I’ve assigned the error messages to errors array. But in case of success messages, after completing a certain action that may be uploading a file or inserting a record in the database. We will assign these success messages to messages array.

Same as how we displayed the error messages, we will display the success messages inside our HTML/PHP Code. First of all, we will check these success messages array is not empty with these PHP functions isset & empty. Then we will use foreach loop to display these success messages.

Adding the Bootstrap Styling to Success Messages

Finally, after displaying these success messages we should add the styling. Otherwise, the user won’t understand the difference, if we use the same styling as the error messages.

For success messages, we will use green colored text or green colored background. With bootstrap, we don’t have to add these styles manually. We can use the bootstrap classes alert and alert-success, for adding this styling.

Complete Code for Displaying Success & Error Messages in PHP

Here is the complete code for displaying success & error messages in PHP

Conclusion

Displaying Success & Error messages in PHP is an important step while working with forms in PHP. And the next step after displaying success & error message in PHP is retaining the user-submitted data in case of error after submitting the form, you will learn about this in the next article. If you want to check the previous articles.

==> What You Should Know about Forms in PHP for Developers
==> How to Add PHP Form Validations

If you have any questions, feel free to post it in the comments section. I’ll reply to every question posted on this blog or on the YouTube channel.


error message coding in php https://codingcyber org/display-success-error-messages-php-8834/ Meindl Contact Us Email mail
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.