Horje
How to Print Success Message in PHP?

In this article, we’ll learn to print a success message using the PHP. so basically we have to show the success messages on the screen. we can assume that the operation is done and now we need to print the success message. so we will learn some methods for this.

These are the following methods:

Using echo or print statements

This method directly displaying the success message by utilizing echo or print statements in PHP. The echo statement outputs the value of the $successMessage variable, which holds the success message, within the HTML markup. Consequently, the success message will appear in the browser if the $operationSuccessful variable is set to true.

Example: This example shows the printing of success message using echo statement.

PHP
<?php
   // Assuming the operation was successful
    $operationSuccessful = true;

if ($operationSuccessful) {
    echo '<div class="success-message">
          Operation was successful!</div>';
}?>

Output
<div class="success-message">
          Operation was successful!</div>

Using a PHP variable in HTML

You can store the success message in a PHP variable and then display it within your HTML markup. The PHP variable $operationSuccessful is assigned the value true, signifying that the operation has been completed successfully. Within the if block, the success message contained in the $successMessage variable is displayed inside a <div> element with the class “success-message”. As $operationSuccessful is true, the code inside the if block is executed, resulting in the success message “Operation was successful!” being displayed within the styled ‘<div>’ element.

Example: This example shows the printing of success message using a PHP variable in HTML.

PHP
<?php
  // Assuming the operation was successful
    $operationSuccessful = true; 
    $successMessage = 'Operation was successful!';

         if ($operationSuccessful) {
    ?>
    <div class="success-message"><?php
         echo $successMessage; ?></div>
    <?php
}?>

Output
    <div class="success-message">Operation was successful!</div>
    

Using printf() function

These functions enable string formatting with variables. Placeholders can be used in your success message and subsequently replaced with actual values through these functions. The PHP variable $operationSuccessful is set to true, signifying that the operation has been completed successfully. Inside the if block, the printf() function is used to format the success message. The %s placeholder in the $successMessage variable is replaced with the string ‘was’ using the printf() function. The message “Operation was successful!” is subsequently displayed within a <div> element assigned the class “success-message”.

Example: This example shows the printing of success message using printf().

PHP
<?php
   // Assuming the operation was successful
    $operationSuccessful = true;
    $successMessage = 'Operation %s successful!';

if ($operationSuccessful) {
    printf('<div class="success-message">' 
    . $successMessage . '</div>', 'was');
}
?>

Output
<div class="success-message">Operation was successful!</div>

Using session variables

You can save the success message in a session variable and display it on subsequent pages or upon page reloads. these are the steps to do that:

  • Start session.
  • Store success message in $_SESSION[‘success_message’].
  • Check if $_SESSION[‘success_message’] is set.
  • Echo success message within <div> if set.
  • Unset $_SESSION[‘success_message’] to display message only once.

Example: This example shows the printing of success message using session variables.

PHP
<?php
session_start();
$_SESSION['success_message'] = 'Operation was successful!';
?>
<!-- On another page or after page reload -->
<?php
if (isset($_SESSION['success_message'])) {
    echo '<div class="success-message">'
        . $_SESSION['success_message'] . '</div>';
  // Clear the session variable
    unset($_SESSION['success_message']); 
}?>

Output
<!-- On another page or after page reload -->
<div class="success-message">Operation was successful!</div>

Note: To use session in any page, we need to start the session on top of the page using the session_start();




Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
Learn Web Development Basics with HTML CSS and JavaScript Learn Web Development Basics with HTML CSS and JavaScript
How to Set Up a Private NPM Registry using Verdaccio ? How to Set Up a Private NPM Registry using Verdaccio ?
How to Retrieve the Request Object in PostMan How to Retrieve the Request Object in PostMan
How to Create Responsive Tables in WordPress ? How to Create Responsive Tables in WordPress ?
How to Restrict File Uploading Permission in WordPress Sites ? How to Restrict File Uploading Permission in WordPress Sites ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12