php 使用html表单将记录添加到mysql数据库后显示弹出消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11296104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:12:12  来源:igfitidea点击:

Display popup message after record has been added to mysql database using html form

phpmysql

提问by dames

I have the following html form:

我有以下 html 表单:

<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

And this is the .php file:

这是 .php 文件:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

How do I get a popup message that displays for 2 seconds after a record has been submitted that says for example "Record added"

如何在提交记录后显示显示 2 秒的弹出消息,例如“已添加记录”

回答by NDM

What you are looking for has nothing to do with PHP, as it is client side...

您正在寻找的与 PHP 无关,因为它是客户端...

There are 2 ways to do this:

有两种方法可以做到这一点:

  • after you submit render the popup and remove it after x time with javascript...
  • submit the form with ajax (javascript) and parse the result, and add popup in javascript.
  • 在您提交渲染弹出窗口并在 x 时间后使用 javascript 将其删除后...
  • 使用ajax(javascript)提交表单并解析结果,并在javascript中添加弹出窗口。

回答by codingbiz

Try the following. You can also enhance it.

请尝试以下操作。你也可以增强它。

On your form page add the css for popup

在您的表单页面上添加用于弹出窗口的 css

//adduser.php
#popup {
    visibility: hidden; 
    background-color: red; 
    position: absolute;
    top: 10px;
    z-index: 100; 
    height: 100px;
    width: 300px
}

and your Popup div

和你的弹出 div

//still adduser.php
<div id="popup">
    Record added successfully
</div>

Output this with PHP after success

成功后用PHP输出

//still adduser.php - probably at the bottom of the page
<?php
    $recordAdded = false;

    if(isset($_GET['status'] && $_GET['status'] == 1)
       $recordAdded = true;

    if($recordAdded)
    {
     echo '
       <script type="text/javascript">
         function hideMsg()
         {
            document.getElementById("popup").style.visibility = "hidden";
         }

         document.getElementById("popup").style.visibility = "visible";
         window.setTimeout("hideMsg()", 2000);
       </script>';
    }
?>

The script unhide the popup to show the message and then hides it after 2 seconds

脚本取消隐藏弹出窗口以显示消息,然后在 2 秒后隐藏它

You can enhance the animation with jQuery and the div with CSS

您可以使用 jQuery 增强动画,使用 CSS 增强 div

UPDATE:

更新:

This assumes you will change your .htmlthat contains the form file to .php

这假设您将.html包含表单文件的内容更改为.php

//insert.php
if (!mysql_query($sql,$con))
{
  //die('Error: ' . mysql_error());
}
else
{
  // 1 record added
  //if number of rows affected > 0
  header("Location: backtoform.php?status=1"); //redirects back to form with status=1
}

Also try out the suggested jQuery/Ajax method mentioned in the comments

还可以尝试评论中提到的建议的 jQuery/Ajax 方法