我有一个用于登录用户的index.php,当按下submit时会发生以下过程.
1.-用户输入一个号码和密码
2.- jQuery.ajax方法用于连接到文件doLogin.php以处理数据,如果正确,则打印"echo‘success“,并使这些变量sessión: 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接收数据以details用户)
因此,出现的问题是:在将数据发送到文件 ()的jQuery.ajax方法()成功之后,该方法对您最好吗?由于名称、姓氏、typeUser和nControl等数据,我希望打印和一个div供用户查看登录的详细信息。
也许我可以使用JSON格式,但不能使用JSON格式。我希望我已经解释过了!
发布于 2012-05-12 17:23:25
// 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-12 18:13:09
没有时间去测试。我在测试服务器上没有php。您将需要jquery.form.js插件的JQuery。
<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";
}https://stackoverflow.com/questions/10565806
复制相似问题