简体   繁体   中英

How to validate the username and password and also the selected role from database using javascript/ajax or any other validation formats

I just want to indicate user your name is wrong ur password is wrong ur role is wrong ., using any validations like ajax/javascript or any other..

index.php

<html>
   <head>
   </head>
   <body>
      <style>
         body {
            width:400px;
             height:auto;
             margin: 0 auto;
             padding-top:20px;
         }
         body a {
             text-decoration:none;
             color:#0099CC;
         }
         .border{
             border:#999999;
             border-style:solid;
             border-width:thin;
         }
      </style>
        <?php
            include('connect.php');
        ?>
      <table width="350" class="border" >
         <form id="form1" name="form1" method="post" action="login.php" onSubmit="return validate()">
            <tr>
               <td width="112">&nbsp;</td>
               <td colspan="2"> <strong>Login</strong></td>
               <td width="22">&nbsp;</td>
            </tr>
            <tr>
               <td height="35">
                  <div align="right">User Name : </div>
               </td>
               <td colspan="2"><label>
                  <input type="text" name="name" id="name" /></label>
               </td>
               <td>&nbsp;</td>
            </tr>
            <tr>
               <td>
                  <div align="right">Password : </div>
               </td>
               <td colspan="2"><input type="password" name="password" id="password" /></td>
               <td>&nbsp;</td>
            </tr>
            <tr>
               <td>
                  <div align="right">select your role : </div>
               </td>
               <td colspan="2">
                  <!--  <input type="radio" checked="checked" name="title" value="member" id="member" /> member</label>
                     <label for="admin"> <input type="radio" name="title" value="admin" id="admin" /> admin</label> -->
                  <label>
                     <select name="member" id="member">
                        <option title="title" value="member"> member
                        <option title="title" value="admin"> admin
                     </select>
                  </label>
               </td>
               <td>&nbsp;</td>
            </tr>
            <tr>
               <td>&nbsp;</td>
               <td width="69"><label>
                  <input type="submit" name="Submit" value="Login" id="add" /></label>
               </td>
               <td width="127"><label>
                  <input type="reset" name="reset" value="reset" /></label>
               </td>
               <td>&nbsp;</td>
            </tr>
            <tr>
               <td>&nbsp;</td>
               <td><a href="register.php">Register ?</a> </td>
               <td><a href="forget_password.php">Forget Password?</a> </td>
               <td>&nbsp;</td>
            </tr>
         </form>
      </table>
      <script>
         function validate()
         {
            if( document.form1.name.value == "" )
            {
                alert( "Please provide your name!" );
                document.form1.name.focus() ;
                return false;
            }
            if( document.form1.password.value == "" )
            {
                alert( "Please provide your password!" );
                document.form1.password.focus() ;
                return false;
            }
         }
      </script>
   </body>
</html>

login.php

<?php
    session_start();
    include('connect.php');
    $name = $_POST['name'];
    $password = $_POST['password'];
    $role = $_POST['member'];
    $sql = "select * from login_register where name = '$name' 
    and password = '$password' and role = '$role'";
    $retain = mysql_query($sql, $con);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($retain);
    $row=mysql_fetch_array($retain);

    if($count == 1)
    {
        if ( $role == "admin")
        {
            $_SESSION['adminuser']=$_POST['name'];
            header("location:admin_home.php");
        }
        else  if ( $role == "member")
        {
            header("location:member_home.php");
            $_SESSION['user']=$_POST['name'];
        }
    }
    else 
    {
        echo  "<a href='index.php'>click here to login   </a>";
    }
?>

I just want to indicate user your name is wrong ur password is wrong ur role is wrong ., using any validations like ajax/javascript or any other.

Here is a sample jquery code. May be it'll help you. It expect the response data in JSON format & then according to the response data value do the rest of execution.

$.ajax({
    type    : "POST",
    data    : { username: "user name", password: "user password", role:"your role" },
    url     : 'some.php',
    dataType: 'json',
    success : function(response) {                    
        console.log( "response data: " + response );                  
    }
});

For more : http://api.jquery.com/jquery.ajax/

<?php
    session_start();
    include('connect.php');


    if(isset($_POST['name']))
    {
    $sql = "select * from login_register where name = ".$_POST['name']; 
    $retain = mysql_query($sql, $con);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($retain);
    if($count < 1)
    {
        echo "Wrong user name";
    }
   }
?>

like this copy and paste for all password and roll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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