2017-04-18 120 views
2

我有一个HTML登录页面,其中有一个管理员和用户的单选按钮,如果用户选择“管理员”,那么它应该重定向到管理员登录页面,如果他选择“用户”那么就应该到用户登录页面html重定向到php

如下面给出的HTML登录页面:

<!-- THE LOG IN PAGE --> 
<div id="id01" class="modal"> 
    <form name="login" id="login" class="modal-content animate" action="login.php" method="post"> 
     <div class="imgcontainer"> 
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span> 
      <img src="images/avatar.png" alt="Avatar" class="avatar"> 
     </div> 
     <div class="container"> 
      <label><b>Email Id:</b></label> 
      <input type="text" placeholder="Enter Email Id" name="email" id="email" required> 
      <label><b>Password:</b></label> 
      <input type="password" placeholder="Enter Password" name="psw" id="psw" required> 
      <label><b>User Type:</b></label> 
      <table width="100%"> 
      <tr> 
      <td><input type="radio" name="usertype" id="usertype" value="admin" required>ADMIN</td> 
      <td><input type="radio" name="usertype" id="usertype" value="user" required>USER</td> 
      </tr> 
      </table>    
      <button type="submit">Login</button> 
      <input type="checkbox" checked="checked"> Remember me 
      <span class="psw"><a href="#">Forgot password?</a></span> 
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button> 
     </div> 
    </form> 
    <script> 
     // Get the modal 
     var modal = document.getElementById('id01'); 
     // When the user clicks anywhere outside of the modal, close it 
     window.onclick = function(event) 
     { 
      if (event.target == modal) 
      { 
       modal.style.display = "none"; 
      } 
     } 
    </script> 
</div> 

为用户admin_login.php页面如下:

<?php 

在session_start() ;

$ email = $ _ POST [“email”];

$ psw = $ _ POST [“psw”];

$ name =“”;

include'db_config.php';

$ sql =“SELECT name,password,email FROM admin_details where email ='$ email'”;

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

如果($ result-> NUM_ROWS> 0)

{

// output data of each row 

while($row = $result->fetch_assoc()) 

{ 

    $name=$row['name']; 

    if($psw == $row['password']) 

    { 

     $_SESSION['name']=$name;  

     $_SESSION['email']=$email; 

     header('Location: admin_index.php'); 

    } 

    else 

    { 

     echo '<script type="text/javascript">'; 

     echo 'alert("WRONG PASSWORD ENTRY. TRY LOGGING IN AGAIN !!!");'; 

     echo 'window.location.href = "admin_index.php";'; 

     echo '</script>';   

    } 

} 

}

别的

{

echo '<script type="text/javascript">'; 

echo 'alert("You must REGISTER first and then LOGIN !!!");'; 

echo 'window.location.href = "admin_index.php";'; 

echo '</script>'; 

}

$ conn-> close();

>

+0

其中是您的PHP脚本用于重定向? –

+0

有两种方法,无论是使用Javascript或PHP(获取元素值,相应地进行比较和重定向) –

+0

男人,这是痛苦的眼睛...请尝试扔它通过在线格式化器或东西,请。这一个是相当不错的,我想http://www.freeformatter.com/html-formatter.html –

回答

0

你先添加和处理程序到你的单选按钮:

<td><input type="radio" name="usertype" id="usertype" value="admin" required>ADMIN</td> 
<td><input type="radio" name="usertype" id="usertype" value="user" required>USER</td> 

<td><input type="radio" name="usertype" id="usertype" onclick="handleUserTypeChanges(this);" value="admin" required>ADMIN</td> 
<td><input type="radio" name="usertype" id="usertype" onclick="handleUserTypeChanges(this);" value="user" required>USER</td> 

然后你可以使用一些JS这样的:

<script type="text/javascript"> 
    function handleUserTypeChanges(radioButton){ 
     var form = document.getElementById("login"); 
     var userType = radioButton.value; 

     if (userType == 'admin') { 
      form.action = 'your/path/to/admin/page/admin.php'; 
     }else{ 
      form.action = 'your/path/to/user/page/user.php'; 
     } 
    } 
</script> 
+0

我不知道为什么,但代码不工作在我的情况 – Neha

+0

我试过了,它的工作原理:https://jsfiddle.net/L5L6hjag/ –

+0

我编辑了我的问题,并插入了admin_login .php页面为admin.Is在admin_login.php页面有任何问题吗? – Neha

0

你可以使用类似的东西。检查的用户类型和redirect accodingly

if(isset($_POST['login'])){ 
    // your code to check email id and password 
    if($_POST['usertype'] == 'admin'){ 
     header('Location: admin.php');   
    }elseif($_POST['usertype'] == 'user'){ 
     header('Location: user.php');  
    }else{ 
     $error = "Invalid Login";   
    }  
} 
0

使用上的login.php文件的代码,希望这将有助于。

if(isset($_POST['email'])) 
{ 

if($_POST['usertype']=='admin') header('location: admin.php'); 
else if($_POST['usertype']=='user') header('location: user.php'); 
exit(); 

} 
0

我在你的while循环中做了一些改变。希望这样可以解决你的问题。

您可以选择管理员或用户的用户角色。在用户角色的行为上,您可以将用户导航到管理页面或登录页面。 这里在数据库中添加另一个属性并插入角色,如果用户选择admin单选按钮,然后在角色属性中设置1,并且用户选择角色属性中的用户单选按钮set 2。

while($row = $result->fetch_assoc()) 

{ 

    $name=$row['name']; 
$psw = $row['password']; 
$role =$row['role']; 
    if($psw == $row['password']) 

    { 

     $_SESSION['name']=$name;  

     $_SESSION['email']=$email; 
if($role == 1){ 
     header('Location: admin_index.php'); 
} 
if($role == 2){ 
     header('Location: admin_index.php'); 
} 

    } 

    else 

    { 

     echo '<script type="text/javascript">'; 

     echo 'alert("WRONG PASSWORD ENTRY. TRY LOGGING IN AGAIN !!!");'; 

     echo 'window.location.href = "admin_index.php";'; 

     echo '</script>';   

    } 

}