简体   繁体   中英

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 eg "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);
}

?>

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

After inserting data you can simply redirect to your interested page with a success message like:

header("location:page_of_interest.php?msg=Record Inserted");

and on page_of_interest.php you can simply check for msg and show if it is set like:

if(isset($_GET['msg'])){
  echo $_GET['msg'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM