PHP 警报消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17747316/
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
PHP alert message
提问by Llama
I know the first thing everyone is going to say is that this has been posted hundreds of times; however, I have tried all the different methods people have suggested with still no luck... I am trying to get an alert message to popup if the login fails but for some reason the message does not want to appear. Here's what I have so far:
我知道每个人首先要说的是,这已经发布了数百次;但是,我已经尝试了人们建议的所有不同方法,但仍然没有运气......如果登录失败,我正试图弹出一条警告消息,但由于某种原因,该消息不想出现。这是我到目前为止所拥有的:
if($conn) {
$result=sqlsrv_query($conn, "SELECT * FROM Operator
WHERE Username='{$_POST['login']}'");
$pass=sqlsrv_fetch_array($result);
if($pass["Password"] === $_POST['password']) {
session_start();
$_SESSION['loggedin']=true;
header("Location: home.php");
exit;
}
else {
header("Location: login.php");
echo '<script type="text/javascript">';
echo 'alert("Password Invalid!")';
echo '</script>';
}
}
sqlsrv_close($conn);
The php is working fine but for some reason I can't get the javascript to work. Any ideas?
php 工作正常,但由于某种原因我无法让 javascript 工作。有任何想法吗?
回答by da-hype
i think, the problem is here. you are redirecting the page before the alert message
我想,问题就在这里。您正在警报消息之前重定向页面
header("Location: login.php"); /* Redirect browser */
try removing it, or make it redirect after a couple of seconds, like this
尝试删除它,或在几秒钟后使其重定向,就像这样
header( "refresh:5;url=login.php" ); //redirect after 5 seconds
or you prompt a dialog for the user to click to go to next page. remove the header() tho
或者您提示一个对话框供用户单击以转到下一页。删除 header() tho
<script type="text/javascript">
<!--
var retVal = confirm("Login FAILED! Do you want to continue ?");
if( retVal == true ){
window.location.href = "login.php";
return true;
}else{
alert("User does not want to continue!");
return false;
}
//-->
</script>
回答by Prasanth Bendra
In your code header("Location: login.php");
is there before your js code so it never execute js code it redirects to login page remove it and try.
在您的代码header("Location: login.php");
中,您的 js 代码之前存在,因此它永远不会执行 js 代码,它会重定向到登录页面,将其删除并尝试。
回答by Chinmay235
Use this code
使用此代码
echo '<script>';
echo 'alert("Password Invalid!");';
echo 'location.href="login.php"';
echo '</script>';
instead of
代替
header("Location: login.php");
echo '<script type="text/javascript">';
echo 'alert("Password Invalid!");';
echo '</script>';
Thanks.
谢谢。
回答by Tdelang
Your code that comes after the header doesn't run, maybe try setting a session variable and check for that variable in login.php. You can then call the alert from there, or just show a simple message on the page.
标头之后的代码没有运行,也许可以尝试设置会话变量并在 login.php 中检查该变量。然后,您可以从那里调用警报,或者只在页面上显示一条简单的消息。
回答by do.becker
there is a missing semicolon right behind the alert:
警报后面缺少一个分号:
if($conn) {
$result=sqlsrv_query($conn, "SELECT * FROM Operator
WHERE Username='{$_POST['login']}'");
$pass=sqlsrv_fetch_array($result);
if($pass["Password"] === $_POST['password']) {
session_start();
$_SESSION['loggedin']=true;
header("Location: home.php");
exit;
}
else {
header("Location: login.php");
echo '<script type="text/javascript">';
echo 'alert("Password Invalid!");';
echo '</script>';
}
}
sqlsrv_close($conn);
didnt you check the javscript console ?
你没有检查 javascript 控制台吗?
EDIT: Sorry, my fault, the semicolon isnt necessary of course.
编辑:对不起,我的错,分号当然不是必需的。
I testet your php code on my server and it works fine anyway !
我在我的服务器上测试了您的 php 代码,无论如何它都可以正常工作!