2017-09-28 49 views
2

我在嘗試弄清楚如何獲取部門在登錄時更改的會話值時出現問題。用戶默認值是在user_info表中設置的。當他們登錄時抓住它並將其存儲在稱爲部門的會話中。這樣我就可以在整個應用程序中使用。我想要做的是讓用戶鍵入一個新部門,並將部門會話更改爲他們在輸入框中輸入的部分。我試圖在profile.php頁面上添加一個$ _Get或$ _POST,因爲一旦用戶登錄就會進入該頁面。我沒有運氣,只是表現爲空白。任何幫助,將不勝感激。我會一直試圖找出答案,如果我得到答案。登錄時試圖獲取會話值更改

的index.php

<div style="width: 300px;"> 
         <div class="panel panel-primary"> 
          <div class="panel-heading">Login</div> 
          <div class="panel-heading"> 
           <label for="lanId">lanId</label> 
           <input type="text" class="form-control" id="lanId"> 
           <label for="lanId">Password</label> 
           <input type="password" class="form-control" id="password" name="password"> 
           <input type="text" class="from-control" placeholder="change department" id="department" name="department"> 

           <p><br></p> 

           <input type="submit" class="btn btn-success" style="float: right;bottom:12px;" id="login" value="Login" name="login"> 
          </div> 

的login.php

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

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

     $lanId=mysqli_real_escape_string($conn,$_POST['lanId']); 
     $pwd=md5($_POST['pwd']); 
     $sql="SELECT * FROM user_info WHERE lanId='$lanId' AND password='$pwd'"; 
     $run_query=mysqli_query($conn,$sql); 
     $count=mysqli_num_rows($run_query); 

     if($count==1){ 
       $row=mysqli_fetch_array($run_query); 
       $_SESSION['uid']=$row['user_id']; 
       $_SESSION['uname']=$row['employee_name']; 
       $_SESSION['dept']=$row['department']; 
       $_SESSION['lanId']=$row['lanId']; 
       echo "true"; 
     } 

    } 

?> 

main.js

$("#login").click(function(event){ 
    event.preventDefault(); 
    var lanId=$('#lanId').val(); 
    var pwd=$('#password').val(); 

    console.log(pwd); 
    $.ajax({ 
     url: "login.php", 
     method: "POST", 
     data: {userLogin:1,lanId:lanId, pwd:pwd}, 
     success: function(data){ 
      if(data=="true"){ 
       window.location.href="profile.php"; 
      } 
     } 
    }) 
}) 

這是個人資料頁的開始。 profile.php

<?php 
    session_start(); 

    if(!isset($_SESSION['uid'])){ 
    header('Location:index.php'); 
    } 

?> 

回答

0

所有你需要在Ajax請求通過你的部門價值就像首先下面

$("#login").click(function(event){ 
event.preventDefault(); 
var lanId=$('#lanId').val(); 
var pwd=$('#password').val(); 
var dept=$('#department').val(); 
console.log(pwd); 
$.ajax({ 
    url: "login.php", 
    method: "POST", 
    data: {userLogin:1,lanId:lanId, pwd:pwd,dept:dept}, 
    success: function(data){ 
     if(data=="true"){ 
      window.location.href="profile.php"; 
     } 
    } 
}) 

})

然後在login.php中

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

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

    $lanId=mysqli_real_escape_string($conn,$_POST['lanId']); 
    $pwd=md5($_POST['pwd']); 
    $sql="SELECT * FROM user_info WHERE lanId='$lanId' AND password='$pwd'"; 
    $run_query=mysqli_query($conn,$sql); 
    $count=mysqli_num_rows($run_query); 

    if($count==1){ 
      $row=mysqli_fetch_array($run_query); 
      $_SESSION['uid']=$row['user_id']; 
      $_SESSION['uname']=$row['employee_name']; 
      $_SESSION['dept']=$row['department']; 
      $_SESSION['lanId']=$row['lanId']; 
      echo "true"; 
    } 
    if(isset($_POST['dept']) && !empyt($_POST['dept'])) 
    { 
     $_SESSION['dept']=$_POST['dept']; 
    } 

} 

?> 

希望它的工作。

+0

這工作除了讓它工作我不得不刪除&&!empyt($ _ POST ['dept']) – Donny