简体   繁体   English

console.log 正在工作,但我无法收到任何警报。 三个警报都没有出现。 需要做什么?

[英]console.log is working but I can't get any alerts. None of the three alerts are showing up. What need to be done?

Please help out, console.log is working but I can't get any alert.请帮忙,console.log 正在工作,但我无法收到任何警报。 None of the three alerts are showing up... Can someone point out the errors or rectify the code?三个警报都没有出现...有人可以指出错误或纠正代码吗? PS: Here is my PHP script any help done would be appreciated. PS:这是我的 PHP 脚本,我们将不胜感激。

<!DOCTYPE html>
<html>
<head>
    <title>MyWebsite xyz</title>
    <link rel="stylesheet" type="text/css" href="Loginstyle.css">
    
    <link rel="stylesheet" type="text/css" href="navbar.css">
    <style type="text/css" id="navstyle2"></style>
    <script src="Loginscript.js" defer></script>
    <script src="navbar.js" defer></script>
</head>
<body>
    <div id="nav"></div>
    <div id="boxx-3">
        <h3>Login</h3>
        <h2 id="msg"></h2>
        <h1 id="hh"></h1>
        <form method="POST">
            <div class="box-3">
                <label>Username</label>
                <input id="usn" class="box-4"  type="text" name="Username" placeholder="Enter your Username">
            </div>
            <br>
            <div class="box-3">
                <label>Password</label>
                <input id="pass" class="box-4"  type="password" name="Password" placeholder="Enter Password">
            </div>
            <br>
            <div class="box">
                <input id="submit" class="submit" type="submit" name="submit" value="Login">
            </div>
        </form>
    </div>
    <?php 
        include ("connect.php");

        $usern = $_POST["Username"];
        $pass = $_POST["Password"];

        $sql="SELECT usern, passn FROM userlist";

        $result = $con->query($sql);

        if(fetch_assoc()){

        while($row = $result->fetch_assoc()) {      
             if($row['passn'] == $pass){
                $ls = 1;
             }
             else{
                $ls = 0;
             }
        }
      }else{
        $ls = 0;
      }
            $con->close();
        ?>
        <script type="text/javascript">
            function login(){

            const username = document.getElementById('usn');
            const password = document.getElementById('pass');
            const msg = document.getElementById('msg');

            if(<?php $ls ?>){
                console.log("success");
                msg.innerHTML = "";
                document.getElementById('boxx-3').innerHTML = "<h4>Logged in successfully !</h4>";
                alert("Logged in successfully !");

            }
            else if(username.value == "" || password.value == ""){
                console.log("empty");
                msg.innerHTML = "* Please fill all the input fields *";
                alert("Please fill all the input fields");
            }
            else{
                console.log("invalid");
                msg.innerHTML = "* Please enter valid username and password *";
                alert("Please enter valid username and password");
            }
            }


            document.getElementById('submit').addEventListener("click", login);
        </script>
</body>
</html>

Can someone point out the errors?有人可以指出错误吗? I have tried clearing the filter cache and even disabling adblocker and such extensions, on chrome to work on this.我已经尝试在 chrome 上清除过滤器缓存,甚至禁用 adblocker 和此类扩展程序来解决这个问题。

if(<?php $ls ?>){

is rendering as if(){ because there is no echo happening.呈现为if(){因为没有回声发生。 I am not crazy about the way you're mixing the JS and PHP, but this might work if you do this:我不喜欢你混合 JS 和 PHP 的方式,但如果你这样做,这可能会起作用:

if(<?php echo $ls ?>){

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

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