简体   繁体   English

PHP警报消息

[英]PHP alert message

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. PHP工作正常,但由于某些原因我无法使JavaScript工作。 Any ideas? 有任何想法吗?

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()寿

<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>

In your code header("Location: login.php"); 在您的代码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. 在您的js代码之前存在,因此它永远不会执行js代码,它会重定向到登录页面,然后删除并尝试。

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. 谢谢。

Your code that comes after the header doesn't run, maybe try setting a session variable and check for that variable in login.php. 标头之后的代码无法运行,请尝试设置会话变量并在login.php中检查该变量。 You can then call the alert from there, or just show a simple message on the page. 然后,您可以从那里呼叫警报,或仅在页面上显示一条简单消息。

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 ? 您没有检查javscript控制台吗?

EDIT: Sorry, my fault, the semicolon isnt necessary of course. 编辑:对不起,我的错,分号当然不是必需的。

I testet your php code on my server and it works fine anyway ! 我在服务器上测试了您的php代码,无论如何它都能正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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