【发布时间】:2014-02-26 23:21:21
【问题描述】:
我已经制作了一个使用 php、ajax 和 mysql 的登录表单,php 代码似乎工作正常,因为它说当输入用户凭据时,屏幕上会回显一条成功消息。
然而,ajax 应该接受并处理它并打开它未能这样做的 index.php 页面。然而,ajax 确实在一定程度上起作用,因为我的“正在检查...”消息按预期出现。任何帮助将不胜感激。请在下面找到代码。
loginajax.js
function chk_ajax_login_with_php() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = "username=" + username + "&password=" + password;
var url = "login.php";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function () {
document.getElementById("status").innerHTML = 'checking...';
},
complete: function () {
},
success: function (html) {
if (html == "success") {
window.location = 'index.php';
}
}
});
}
login.php
<?php
require "header.php";
try
{
$connection = mysql_connect("localhost","root", "dbpassword");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("dbname",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}catch (Exception $e){
error_log(" DB Error: ".$e->getMessage());
}
if($_POST){
$username=$_POST['username'];
$password=$_POST['password'];
$sql = mysql_query("SELECT * FROM usertable WHERE userID ='".$username."' AND userPass ='".md5($password)."'") or die(mysql_error());
$res=mysql_num_rows($sql);
if($res>0){
$rs_login=mysql_fetch_assoc($sql);
$uid=$rs_login['user_id'];
$_SESSION['sess_uid']=$uid;
echo "success";
}
else{
echo "invalid username or password";
}
}
?>
【问题讨论】:
-
你确定服务器端脚本返回 echo "success";
-
在
login.php中尝试echo "success";exit; -
嗨,是的,我尝试回显成功并显示消息。
-
但是问题仍然存在,页面没有被 index.php 页面替换。
-
success这个词前后有空格吗?