在登录表单上显示错误消息

时间:2015-03-23 05:26:47

标签: javascript php sql ajax mysqli

我已经为网站创建了一个登录页面。表单的代码及其后端代码在同一页面上

db表的视图是

id        email         password   status   block
1   vendor@gmail.com     vendor    vendor   active
2    ABC@gmail.com        ABC      vendor   inactive

表格代码是     

        $myemail = mysqli_real_escape_string($con, $_POST['email']);
        $mypassword = mysqli_real_escape_string($con, $_POST['password']);
        $sql = "SELECT id FROM register WHERE email='$myemail' and password='$mypassword' and status='vendor' and block='inactive'";
        $result = mysqli_query($con, $sql);

        if (mysqli_num_rows($result) > 0)
            {
                while($row = mysqli_fetch_assoc($result)) 
                    {
                        $_SESSION['login_user']= $row["email"];
                        header("location: vendorpanel.php");
                    }
            }

        else 
            {
                $error="Your Login Name or Password is invalid";
            }
    }
?>

<form class="login-form" action="" method="POST">
    <h1>Login Form</h1>
    <div>
        <input type="text" placeholder="email" required="" id="email" name="email" />
    </div>
    <div>
        <input type="password" placeholder="Password" required="" id="password" name="password"/>
    </div>
    <div>
        <input type="submit" value="Log in" name="submit" />
        <!--<a href="#">Lost your password?</a>
        <a href="#">Register</a>-->
    </div>
</form>

登录代码工作正常,但我想将两种类型的错误显示为弹出框。

1) the first error is if the password is wrong then it should display a password error
2) the second error is if the block parameter is active then the user should get a message that his account has been blocked

任何人都可以告诉我怎么做。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码,它可以帮助您

 if($_SERVER["REQUEST_METHOD"] == "POST")
    {

        $sql = "SELECT id FROM register WHERE email='$myemail' and password='$mypassword' and status='vendor' and block='inactive'";
        $result = mysqli_query($con, $sql);
        if (mysqli_num_rows($result) > 0)
        {
               while($row = mysqli_fetch_assoc($result)) 
               {
                        $_SESSION['login_user']= $row["email"];
                        header("location: vendorpanel.php");
               }
            }

        else 
        {
              $q=mysql_query("SELECT id,password,block FROM register WHERE email='$myemail' || password='$mypassword' || status='vendor' || block='inactive'");
                if(mysql_num_rows($q)>0)
                {
                $password=mysql_result($q,0,"password");
                $block=mysql_result($q,0,"block");
                }
                if($block=='active')
                {
                   echo $error='you Account is blocked';
                }
               else if($password!='$mypassword')
               {
                   $error="Your Login Password is invalid";
               }
               else
              {
                    $error="Your Login Email is invalid";
               }
        }
    }

答案 1 :(得分:0)

在你的php代码中重新开始,

 <?php 
if($_SERVER["REQUEST_METHOD"] == "POST")
    {
 $isblock = false;
 $wrong_user_pwd = false;
        $sql = "SELECT id,block FROM register WHERE email='$myemail' and password='$mypassword' and status='vendor'";
        $result = mysqli_query($con, $sql);

        if (mysqli_num_rows($result) > 0)
            {
                while($row = mysqli_fetch_assoc($result)) 
                    {
                        $_SESSION['login_user']= $row["email"];
                        if($row['block'] == 'active')
                        $isblock = true;
                        header("location: vendorpanel.php");
                    }
            }

        else 
            {
                $wrong_user_pwd= true;
            }
    }

   if($wrong_user_pwd) 
       echo "Your Login Name or Password is invalid";
  if($isblock)    
     echo "User Blocked";
?>