Drupal 7: Adding Drupal set messages using PHP(drupal_set_message()) and Javascript:

In Drupal messages are printed using the PHP function drupal_set_messages(). This article will discuss the adding message to page through the PHP as well as Javascript. The normally the set message will create a div HTML wrapper for display the messages. Using the classes of the set message we can display the Error/Status/Warning message to the end users.

PHP(drupal_set_message()):

// Status Message.
drupal_set_message("Welcome to CodeExpertz!", 'status');
 
// Warning Message.
drupal_set_message("CodeExpertz is a Best Drupal Blog!", 'warning');
 
// Error Messages.
drupal_set_message("CodeExperetz only discuss about technical stuffs!", 'error');

JavaScript(append):

// Status Message.
$("body").append('< div class="messages status">Welcome to CodeExpertz!<!-- div-->');
 
// Warning Message.
$("body").append('< div class="messages warning">CodeExpertz is a Best Drupal Blog!<!-- div-->');
 
// Error Messages.
$("body").append('< div class="messages error">CodeExperetz only discuss about technical stuffs!<!-- div-->');
Category: