2017-04-18 100 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>';   

    } 

}