php 提交后成功/失败消息弹出框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16250596/
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
successful/fail message pop up box after submit?
提问by munue
Basically after clicking the submit button, I want a pop up box to pop up saying successful or fail, then clicking OK to confirm the message. At the moment i am getting a pop up box "undefined" followed by failed message pop up box. HELP PLEASE!
基本上在单击提交按钮后,我想要一个弹出框弹出说成功或失败,然后单击确定以确认消息。目前我收到一个弹出框“未定义”,然后是失败的消息弹出框。 请帮忙!
here is the script
这是脚本
<?php
include ('config.php');
if (isset($_POST['name'])) {
$name = "name";
$query = "INSERT INTO pop ('id','name') VALUES ('','$name')";
$result = mysql_query($query,$cn);
if ($result) {
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
else
{
echo "<script type='text/javascript'>alert('failed!')</script>";
}
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
Name:<input type="text" id="name" name="name"/>
<input type="submit" value="submit" name="submit" onclick="alert();"/>
</form>
</body>
回答by DannyB
You are echoing outside the body tag of your HTML. Put your echos there, and you should be fine.
您正在 HTML 的 body 标记之外进行回显。把你的回声放在那里,你应该没问题。
Also, remove the onclick="alert()"
from your submit. This is the cause for your first undefined
message.
另外,onclick="alert()"
从您的提交中删除。这是您undefined
发送第一条消息的原因。
<?php
$posted = false;
if( $_POST ) {
$posted = true;
// Database stuff here...
// $result = mysql_query( ... )
$result = $_POST['name'] == "danny"; // Dummy result
}
?>
<html>
<head></head>
<body>
<?php
if( $posted ) {
if( $result )
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
else
echo "<script type='text/javascript'>alert('failed!')</script>";
}
?>
<form action="" method="post">
Name:<input type="text" id="name" name="name"/>
<input type="submit" value="submit" name="submit"/>
</form>
</body>
</html>
回答by half-fast
Instead of using a submit button, try using a <button type="button">Submit</button>
不要使用提交按钮,而是尝试使用 <button type="button">Submit</button>
You can then call a javascript function in the button, and after the alert popup is confirmed, you can manually submit the form with document.getElementById("form").submit(); ... so you'll need to name and id your form for that to work.
然后就可以在按钮中调用一个javascript函数,确认弹出alert后,就可以用document.getElementById("form").submit();手动提交表单了。...所以你需要命名和标识你的表单才能工作。