我的网站有一个奇怪的问题。经过整整2天的努力解决后,我仍然没有设法找到错误。
我最近注意到(我认为它是在更改服务器之后开始的)我的ajax
代码不再正常工作了。在它像魅力一样工作之前。以下是我在线社区的登录脚本示例:
遵循表格:
<form class="registration-form" name="signupform" onsubmit="return false;">
<input type="text" onfocus="emptyElement('status')" placeholder="Email" id="email" autofocus>
<input type="password" onfocus="emptyElement('status')" placeholder="Password" id="password">
<button type="submit" id="loginbtn" onclick="login()" >Log in!</button>
<div id="status"></div>
</form>
关注JS:
function emptyElement(x){
_(x).innerHTML = "";
}
function login(){
var e = _("email").value;
var p = _("password").value;
if(e == "" || p == ""){
_("status").innerHTML = 'Please fill out all the form data.';
} else {
_("loginbtn").style.display = "none";
_("status").innerHTML = 'Loading...';
var ajax = ajaxObj("POST", "login.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "login_failed"){
_("status").innerHTML = 'Login unscuccessful';
_("loginbtn").style.display = "block";
} else {
window.location = "xxxx";
}
}
}
ajax.send("e="+e+"&p="+p);
}
}
关注PHP:
if(isset($_POST["e"])){
include_once("php_includes/db_conx.php");
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$e = strtolower($e);
$p = md5($_POST['p']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
if($e == "" || $p == ""){
echo "login_failed";
exit();
} else {
$query = mysqli_query($db_conx,"SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1");
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
echo "login_failed";
exit();
} else {
//set cookies and log user in
}
}
exit();
}
虽然表单的php
正在运行,但当用户登录时所有值都正确时,ajax
没有显示任何状态或加载消息,就像上面的代码一样。< / p>
脚本没有其他任何改变。我仍然像以前一样在页面顶部添加ajax.js
和jquery
。
这只是一个例子。我用来让人们注册的表单和这个表单一样,并且有同样的问题,就像我网站上的任何其他ajax
表单一样。
问题是什么?
答案 0 :(得分:1)
在从服务器获取数据到javascript(AJAX)时更好地使用json
if(isset($_POST["e"])){
include_once("php_includes/db_conx.php");
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$e = strtolower($e);
$p = md5($_POST['p']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
$json['success'] = true;
if($e == "" || $p == ""){
$json['success'] = false;
} else {
$query = mysqli_query($db_conx,"SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1");
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
$json['success'] = false;
} else {
//set cookies and log user in
}
}
// display your return data as json
echo json_encode($json);
}
将onreadystatechange函数的内部包装到
if (this.readyState == 4 && this.status == 200) {
}
删除您的条件
if(ajaxReturn(ajax) == true) {
}
您的js文件现在就像这样
function emptyElement(x){
_(x).innerHTML = "";
}
function login(){
var e = _("email").value;
var p = _("password").value;
if(e == "" || p == ""){
_("status").innerHTML = 'Please fill out all the form data.';
} else {
_("loginbtn").style.display = "none";
_("status").innerHTML = 'Loading...';
var ajax = ajaxObj("POST", "login.php");
ajax.onreadystatechange = function() {
// when ajax is now okay and request finished and response is ready
if (this.readyState == 4 && this.status == 200) {
var result = JSON.parse(this.responseText);
// debugging purpose display response
console.log(result);
if(result.success == true){
_("status").innerHTML = 'Login unscuccessful';
_("loginbtn").style.display = "block";
} else {
window.location = "xxxx";
}
}
}
ajax.send("e="+e+"&p="+p);
}
}