我的index.php文件(www root)中有以下jQuery:
$(document).ready(function() {
$('#loginForm').submit(function() {
var username = $("#username").val();
var password = $("#password").val();
$.post("php/login.php",
{
username: username,
password: password
},
function(data,status){
$("#loginstatus").html(data);
alert(data);
}
);
});
});

和php文件夹中的以下php脚本:
<?php
$username = isset($_POST["username"]) ? $_POST["username"] : '' ;
$password = isset($_POST["password"]) ? $_POST["password"] : '' ;
$dbhost = "localhost";
$dbuser = "s150314";
$dbpass = "password";
$dbname = "users";
$loginerror = "";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$sqlu = "SELECT username FROM users WHERE username LIKE '$username' ";
$sqlp = "SELECT password FROM users WHERE password LIKE '$password' ";
$sqlfn = "SELECT FirstName,LastName FROM users WHERE username LIKE '$username' ";
$resultu = mysqli_query($conn, $sqlu) ;
$resultp = mysqli_query($conn, $sqlp) ;
$resultfn = mysqli_query($conn, $sqlfn) ;
if (mysqli_num_rows($resultu) > 0 && mysqli_num_rows($resultu) > 0 ) {
while($row = mysqli_fetch_assoc($resultfn)) {
$_SESSION["FirstName"] = $row["FirstName"];
}
echo "Success!";
header("Location: ../pages/redirect.php");
} else {
echo "Error!";
}
?>
&#13;
然而,表格的提交没有变化。理论上我应该被重定向到redirect.php ...如果我将php代码替换回index.php它可以完美地工作。
答案 0 :(得分:1)
<script>
$.post("php/login.php",
{
username: username,
password: password
},
cache:false,
success:function(data){
if(data.status == 'success'){
window.location.href="http:\\www.site_name.php/pages/redirect.php"";
}else if(data.status == 'error'){
alert(data);
}
}
</script>
<强> PHP 强>
<?
header('Content-type: application/json');
$SqlQuery = "SELECT FirstName,LastName,username FROM users WHERE username LIKE '$username' AND password LIKE '$password'";
$resultu = mysqli_query($conn, $SqlQuery) ;
if (mysqli_num_rows($resultu) > 0 && mysqli_num_rows($resultu) > 0 ) {
while($row = mysqli_fetch_assoc($SqlQuery)) {
$_SESSION["FirstName"] = $row["FirstName"];
}
$response_array['status'] = 'success';
} else {
$response_array['status'] = 'error';
}
?>