Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript/PHP: Alert dialog after successfully saved

I new in programming. Currently, I develop a system that registration part. The registration part is successfully saved to the database. What I want to know is how to popup an alert dialog with one button e.g "Ok" after registration was successful and redirect to another page, such as home page. Now I only echo "successfully saved"

Below is my current code

<?php
require "DbConnect.php";
    $name = $_POST['name'];
    $badgeid = $_POST['badgeid'];
    $position = $_POST['position'];
    $department = $_POST['department'];
    $factory = $_POST['factory'];
    $reviewer = $_POST['reviewer'];
    $title = $_POST['title'];
    $year = $_POST['year'];
    $month = $_POST['month'];
    $suggestionwill = $_POST['suggestionwill'];
    $present = $_POST['present'];
    $details = $_POST['details'];
    $benefit = $_POST['benefit'];


$sql_query = "INSERT INTO topsuggest (name,badgeid,position,department,factory,
reviewer,title,year,month,suggestionwill,present,details,benefit) VALUES('$name','$badgeid','$position','$department','$factory','$reviewer','$title','$year','$month','$suggestionwill','$present','$details','$benefit')";

if(mysqli_query($conn,$sql_query))
{
echo "<p id='msg'></p>";
}
else
{
echo "Error!! Not Saved".mysqli_error($con);
}

?>

1 Answers

Just use php header and use javascript to alert a message .

      if(mysqli_query($conn,$sql_query))
    {
    echo "<script>alert('Successfuly Saved');</script>";
 header('Location: PATH TO BE REDIRECTED');
    }

For a example

if(mysqli_query($conn,$sql_query))
    {
    echo "<script>alert('Successfuly Saved');</script>";
 header('Location: ../Insert/Index.php');
    }

Please note that space between Location: is compulsory

like image 153
Nipun Tharuksha Avatar answered Nov 27 '25 10:11

Nipun Tharuksha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!