JavaScript / PHP:成功保存后的“警告”对话框

时间:2019-06-20 07:19:28

标签: javascript php html

我是编程新手。目前,我正在开发一个注册部分的系统。注册部分已成功保存到数据库。我想知道的是如何在注册成功后用一个按钮(例如“确定”)弹出一个警告对话框,然后重定向到另一页(例如主页)。现在我只回显“成功保存”

下面是我当前的代码

<?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);
}

?>

2 个答案:

答案 0 :(得分:2)

只需使用php标头并使用javascript来提醒消息。

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

例如

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

请注意,“位置:”之间的空格是必填项

答案 1 :(得分:2)

After inserting data you can simply redirect to your interested page with 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'];
}