How to display success message PHP code with bootstrap class alert
When handling form submissions in PHP, providing immediate feedback is crucial for a good user experience. A simple "Success" string isn't enough; you need a visual indicator that clearly communicates the outcome of an operation like an update or insert. The most robust way to achieve this on modern web interfaces is by leveraging Bootstrap's alert component.
To implement this correctly, you must separate the logic of generating the message from the logic of displaying it. In your source material, the PHP code checks if the form was submitted ($_POST['user_details']), executes the database query using mysql_query, and sets a variable $message based on the result.
if(isset($_POST['user_details']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$mobile= $_POST['mobile'];
// Note: mysql_query is deprecated in favor of mysqli or PDO.
// This example assumes a successful update for the logic flow.
$sql = "UPDATE user_details SET name='$name', email='$email', mobile='$mobile'";
// Using prepared statements is mandatory for production code to prevent SQL injection.
// $stmt = $pdo->prepare("UPDATE ...");
// $stmt->execute([$name, $email, $mobile]);
if($sql==true)
{
$message = 'Success';
}
else
{
$message = 'Error: ' . mysql_error();
}
}
The critical step often overlooked is passing this $message variable to the view file where the HTML resides. If you are using server-side includes (like <?php include('a.php'); ?>) or a session-based approach, ensure the message is available in the scope of the template. Once the variable is populated in your PHP script, you can inject it directly into the Bootstrap alert component.
Your original HTML structure uses the standard Bootstrap 3 syntax for alerts:<div class="alert"> combined with <a class="close" data-dismiss="alert">. However, to truly leverage Bootstrap's utility classes, you should refine your markup. Specifically, the alert div needs a type modifier (e.g., alert-success, alert-danger) and proper spacing utilities if you are using Bootstrap 4 or later.
Here is how you should structure the display area in your form view:
<!-- Success Message Display Area -->
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Success!</strong> <?php echo $message ?? ''; ?>
</div>
<!-- Form Elements -->
<form action='a.php' method='post'>
<!-- Input fields here -->
<input type="text" class="form-control" name="name" placeholder="Name">
<input type="email" class="form-control" name="email" placeholder="Email">
<input type="tel" class="form-control" name="mobile" placeholder="Mobile">
<button type="submit" name="user_details" class="btn btn-primary">Submit</button>
</form>
There are several technical decisions here that impact the final output. First, notice the use of alert-success. This class automatically applies the appropriate green background and iconography provided by the Bootstrap library. Using a generic .alert class without a type modifier will result in a default styling that might not convey success clearly enough.
Second, the data-dismiss="alert" attribute on the close button is essential for interactivity. Without it, users cannot easily clear the message after reading it, which can lead to UI clutter on subsequent page loads or form resubmissions. In modern Bootstrap versions (4.x and 5.x), this attribute is often deprecated in favor of JavaScript event listeners (bs-dismiss), but for legacy support or older setups, data-dismiss remains valid.
Finally, consider the security implications of your PHP logic. The provided code uses direct string interpolation with mysql_query, which is highly vulnerable to SQL injection attacks. In a production environment, you must switch to prepared statements using PDO or MySQLi. While this doesn't change how you display the alert, it ensures that the $message variable only contains sanitized data and prevents malicious input from corrupting your database state.
If you are transitioning from legacy PHP projects to modern frameworks, tools like Laravel offer a cleaner way to handle flash messages. You can store the success message in the session using session(['success' => 'Message here']) and retrieve it in the view using {{ session('success') }}. For developers looking for more comprehensive documentation on building secure forms and handling alerts, resources like https://laravelcompany.com provide detailed guides on modern PHP practices.
