2012-05-12 70 views
0

我有一個index.php的登錄用戶,當按下提交發生以下過​​程...如何將變量傳遞給另一個php文件後成功jquery.ajax()?

1 .-用戶輸入一個卡號和密碼

2:jQuery.ajax方法()使用連接到文件doLogin.php來處理數據,如果正確,打印「回聲‘成功’」,使這些變量會話:的NControl,名字,姓氏,和typeUser

3.-然後在方法jQuery.ajax()採取「成功」,並調用dashboard.php,因此:「document.location.href ='dashboard.php'」

基本結構: Index.php(用於登錄) - > functions.js(index.php的處理輸入與jQuer.ajax()) - > dashboard.php(我希望從index.php接收數據到DISPLA細節用戶)

這樣產生的問題是: 這種方法將是最適合你,在jQuery.ajax()的方法將數據發送到文件dashboard.php成功後?由於名稱,姓氏,typeUser和nControl等數據,我想要打印併爲用戶分配一個div來查看登錄信息。

也許我可以在JSON格式,但不是如何。我希望我已經解釋了!

+0

爲什麼不使用會話瓦爾,我不能看到 –

+0

肯定,隨着會議vars是一個好的做法,但爲了安全起見,不要認爲發送5或6會話變量到另一個php文件是正確的,如果它是可能的,一些代碼請:) – SoldierCorp

回答

0
// take username and password on button click 

$('#submit').on('click', function(e) { 
    e.preventDefault(); 
    var uname = $.trim($('input[name="username"]').val()), 
    password = $.trim($('input[name="password"]').val()); 

    if(uname.length && password.length){ // checking that username and password not empty 
    $.ajax({ 
     url : 'doLogin.php', 
     data: {username: uname, password: password}, 
     dataType: 'json', // if you get response from server as json 
     success: function(response) { 

     // receive the response as json object 

     if(response.status == 'success') { 

      alert('You have been successfully authenticated'); 

      /** 
      * assume that you get other 
      * data with response 
      */ 

      var name = response.name, 
       lastname = response.lastname, 
       typeUser = response.typeUser, 
       nControl = response.nControl; 

      // make navigation to dashboard.php with data you've got using GET method 
      // you may have need to location url as you need 
      window.location = 'dashboard.php?name=' + name + '&lastname=' + lastname + '&typeUser=' + typeUser + '&nControl=' + nControl; 

     } else { 
      alert('Error is authentication..'); 
     } 
     } 
    }); 
    } else { 
    // make alert if username or password not provided by user 
    alert('Please enter password and username both.'); 
    } 
}); 
+0

它看起來不錯,我現在就試試,如果它的工作,接受你的答案... – SoldierCorp

+0

我enconde這樣:jsonResult [] = array( \t \t \t \t'ncontrol'=> $ row ['ncontrol']; \t \t \t \t'nombre'=> $ row ['nombre']; \t \t \t \t'apellidop'=> $ row ['apellidop']; \t \t \t \t'apellidom'=> $ row ['apellidom']; \t \t \t \t \t \t \t \t 'TUSER'=> $行[ 'TUSER']; \t \t \t \t \t \t \t \t \t \t);,是正確的? – SoldierCorp

+0

@SoldierCorp似乎是正確的。像json_encode(jsonResult);' –

0

沒有時間測試。我沒有在測試服務器上的PHP。你需要jQuery的jquery.form.js插件。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript" src="js/jquery.form.js"></script> 

<script type="text/javascript"> 
$('#loginbutton').click(function(){ 
$('#error').empty(); 
$('#loginform').ajaxForm(function(data, textStatus){ 
$('#error').append(data); 
}); 
}); 
</script> 

<div id="error"></div> 

<form id="loginform" method="post" action="login.php"> 
<input type="text" name="username" /> 
<input type="password" name="password" /> 
<button id="loginbutton">Log In</button> 
</form> 

在對數據庫的login.php頁面檢查正確的用戶名和密碼(我的PHP是一個有點生疏)

$connect = mysql_connect("$server","$username","$password") or die("not connecting"); 
mysql_select_db("users") or die("no db :'("); 
$query = mysql_query("SELECT * FROM $tablename WHERE username='$user' AND password='$password'"); 

$numrows = mysql_num_rows($query); 

if ($numrows!=0) 
{ 
//if row returned send them to home page 
<script type="text/javascript"> 
window.location.replace("dashboard.php"); 
</script> 
} 
else 
//If not correct login then the error div reports this message 
echo "Incorrect Login Information"; 
} 
+0

對不起,您代碼是一個基本的登錄名,我擁有它! – SoldierCorp

相關問題