我需要使用jSON和Ajax登录数据库以将其刷新到自身。以下是执行此操作的固定代码:
< auth.php>`
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"
type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript">
$(document).ready(function() {
$("#login").click(function() {
var action = $("#form1").attr('action');
var form_data = {
email: $("#email2").val(),
password: $("#password").val(),
};
$.getJSON("login.php",form_data,function(data){
switch(data.retval){
case 0: $("#form").html("You have logged in successfully,"+ data.data.displayName+"!");
break;
case 1: $("#status").html("Invalid Username/Password, please try again.");
break;
default: $("#status").html("Database error, please try again.");
break;
}
});
});
});
</script>
</head>
<body>
<div id="fullBG">
<div id="formBG">
<div id="form">
<div id="status">
<br />
<br />
</div>
<form id="form1" name="form1" action="login.php" method="post">
<input type="email" placeholder="Email" name="email" id="email2" size="30">
<br>
<input type="password" placeholder="Password" name="password" id="password"
size="30">
<br>
<input type="button" value="Login" class="sub" id="login">
</form>
<br />
<br />
<form method="link" action="register.php">
<input type="submit" value="Register" class="register">
</form>
</div>
</div>
</div>
</body>
</html>`
&LT; login.php&gt;`
<?php
ob_start();
session_start();
mysql_connect('', '', '') or
die('Could not connect: ' . mysql_error());
mysql_select_db('phppro2') or
die ('Can\'t use database: ' . mysql_error());
// retval: 0 - login ok, 1 - login failed, 2 - internal error
$json = array("retval" => 2, "data" => NULL, "debug" => "");
$email = mysql_real_escape_string($_REQUEST['email']);
$password = mysql_real_escape_string($_REQUEST['password']);
$sql="SELECT * FROM users WHERE email='$email' and password='$password'";
$json['debug'] .= "SQL query was: ".$sql."\n";
$result=mysql_query($sql);
if (!$result) {
$json['debug'] .= "SQL query failed\n";
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
die(json_encode($json));
}
$count=mysql_num_rows($result);
if($count==1){
$json['retval'] = 0;
$json['data'] = mysql_fetch_assoc($result);
} else {
$json['retval'] = 1;
}
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
echo json_encode($json);`
//老问题:
我需要从我的数据库中检索信息并使用Ajax加载它。我有一切正常,但我的auth.php文件被覆盖,我丢失了。我试图回溯我的步骤,但我迷路了。页面不再刷新自身,也不会检索数据。 (我删除了我的数据库信息,但它已连接)
我的login.php我没有改变,所以我认为它是准确的。
答案 0 :(得分:0)
1)您不应该保留两个按钮(注册和登录按钮)的相同ID
2)更改你的javascript功能如下
$.getJSON("login.php",form_data,function(data){
switch(data.retval){
case 0: $("#form").html("You have logged in successfully,"+ data.data.displayName+"!");
break;
case 1: $("#status").html("Invalid Username/Password, please try again.");
break;
default: $("#status").html("Database error, please try again.");
break;
}
}
3)你的状态div也必须是空白
<div id="status"></div>
4)http://samaradionne.com/phppro2/auth.php
上的语法错误$(document).ready(function() {
$("#login").click(function() {
var action = $("#form1").attr('action');
var form_data = {
email: $("#email").val(),
password: $("#password").val(),
};
$.getJSON("login.php",form_data,function(data){
switch(data.retval){
case 0: $("#form").html("You have logged in successfully,"+ data.data.displayName+"!");
break;
case 1: $("#status").html("Invalid Username/Password, please try again.");
break;
default: $("#status").html("Database error, please try again.");
break;
}
});
});
});