0

我有一个用于登录用户的 index.php,当按下提交时,会发生以下过​​程......

1.- 用户输入号码和密码

2.- 使用 jQuery.ajax 方法 () 连接到文件 doLogin.php 以处理数据,如果正确,则打印“echo 'success'”并使这些变量会话:nControl、name、lastname 和 typeUser

3.- 然后在 jQuery.ajax () 方法中获取“成功”并调用dashboard.php,因此:“document.location.href = 'dashboard.php'”

基本结构: Index.php(用于登录)->functions.js(使用 jQuer.ajax() 处理 index.php 的输入)->dashboard.php(我想从 index.php 接收数据以显示详细信息用户)

所以出现的问题是: 那个方法最适合你,在jQuery.ajax方法()成功后将数据发送到dashboard.php文件?因为像name、lastname、typeUser和nControl这样的数据,我想打印一个div,让用户看到你登录的详细信息。

也许我可以是 JSON 格式,但不是。我希望我已经解释了!!

4

2 回答 2

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.');
  }
});
于 2012-05-12T17:23:25.370 回答
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";
}
于 2012-05-12T18:13:09.820 回答