我正在使用ajax概念处理登录表单请求。下面我发布了与登录相关的代码。它不会重定向到myaccount.php页面。如果我使用这个echo'<script>window.location="myaccount.php";</script>';
javascript函数,它将正常工作。但是,这里我想用PHP做。 它将myaccount.php结果作为ajax消息返回。
我正在使用此<div id="loginreturn"></div>
收到错误消息。上面我发布了一个错误图片。 myaccount.php页面结果也显示在<div id="loginreturn"></div>
内。那是我的问题。它不会重定向到另一个php页面。如何解决这个错误?
的login.php
<?php
ob_start();
include('config.php');
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$user_email = $_POST['email'];
$user_password = $_POST['password'];
$_SESSION['ses_uemail'] = $user_email;
if(empty($user_email) || empty($user_password) )
{
echo "You must provide your email id and password";
die();
}
try
{
$stmt = $conn->prepare("SELECT * FROM table_name WHERE EmailID = ? AND Password = ?");
$conn->errorInfo();
$stmt->bindParam('1', $user_email, PDO::PARAM_STR);
$stmt->bindParam('2', $user_password, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch())
{
$rename = $row['Name'];
$reemail = $row['EmailID'];
$repassword = $row['Password'];
}
if($reemail == $_SESSION['ses_uemail'] && $repassword == $user_password)
{
$_SESSION['ses_name'] = $rename;
$_SESSION['ses_email'] = $reemail;
$_SESSION['ses_password'] = $repassword;
header('Location:myaccount.php');
}
else
{
echo "Incorrect email id and password";
}
}
catch(PDOException $e)
{
'Error : ' .$e->getMessage();
}
}
ob_flush();
?>
ajax.js
// ajax signin
jQuery(document).ready(function() {
jQuery("#registertologin").click(function() {
jQuery("#loginreturn").html("<img src='img/loading.gif'/>");
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax( {
url : 'login.php',
type: "POST",
data : $('#loginform').serialize(),
success:function(data, textStatus, jqXHR) {
jQuery("#loginreturn").html('<pre><code class="returndata">'+data+'</code></pre>');
$("#loginform").submit_login(); //SUBMIT FORM
},
error: function(jqXHR, textStatus, errorThrown)
{
$("#loginreturn").html('<pre><code class="returndata">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
}
});
e.preventDefault(); //STOP default action
});
});
的index.php
<form name="loginform" id="loginform" >
<table class="outlineborder" width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="35" colspan="2">
<div class="signinform">SignIn Form</div>
</td>
</tr>
<tr>
<td height="35" colspan="2"> </td>
</tr>
<tr>
<td width="150" height="35" class="rightalign">Email Id : </td>
<td width="348"><input class="inputfield" type="text" name="email" /></td>
</tr>
<tr>
<td height="35" class="rightalign">Password : </td>
<td><input class="inputfield" type="password" name="password" /></td>
</tr>
<tr>
<td height="35" colspan="2" align="right">
<span class="forgetpassword">Forget Password ?</span>
</td>
</tr>
<tr>
<td height="35" colspan="2" align="center">
<div id="loginreturn"></div>
</td>
</tr>
<tr>
<td height="35" colspan="2" align="center">
<input class="btns" type="button" name="submit_login" id="registertologin" value="SignIn" />
<input class="btns" type="button" value="Close" onclick="document.getElementById('login_signup').style.display='none'; document.getElementById('fade').style.display='none'" />
</td>
</tr>
</table>
</form>
答案 0 :(得分:2)
'login.php'的变化
while($row = $stmt->fetch())
{
$rename = $row['Name'];
$reemail = $row['EmailID'];
$repassword = $row['Password'];
}
if($reemail == $_SESSION['ses_uemail'] && $repassword == $user_password)
{
$_SESSION['ses_name'] = $rename;
$_SESSION['ses_email'] = $reemail;
$_SESSION['ses_password'] = $repassword;
return true;
}else
{
return false;
}
'ajax.js'的变化
success:function(data, textStatus, jqXHR) {
if(data == true)
{
window.location.href="myaccount.php"
}else
{
jQuery("#loginreturn").html('<pre><code class="returndata">'+data+'</code></pre>');
$("#loginform").submit_login(); //SUBMIT FORM
}
},
也许它可以帮助你...
由于
答案 1 :(得分:0)
试试这个,
<强>的login.php 强>
<?php
ob_start();
include('config.php');
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
$msg = array();
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$user_email = $_POST['email'];
$user_password = $_POST['password'];
$_SESSION['ses_uemail'] = $user_email;
if(empty($user_email) || empty($user_password) )
{
echo "You must provide your email id and password";
die();
}
try
{
$stmt = $conn->prepare("SELECT * FROM table_name WHERE EmailID = ? AND Password = ?");
$conn->errorInfo();
$stmt->bindParam('1', $user_email, PDO::PARAM_STR);
$stmt->bindParam('2', $user_password, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch())
{
$rename = $row['Name'];
$reemail = $row['EmailID'];
$repassword = $row['Password'];
}
if($reemail == $_SESSION['ses_uemail'] && $repassword == $user_password)
{
$_SESSION['ses_name'] = $rename;
$_SESSION['ses_email'] = $reemail;
$_SESSION['ses_password'] = $repassword;
//header('Location:myaccount.php'); //no need
return json_encode(array('error'=> false));
}
else
{
//echo "Incorrect email id and password";
return json_encode(array('error'=> true, 'msg'=> "Incorrect email id and password"));
}
}
catch(PDOException $e)
{
//'Error : ' .$e->getMessage();
return json_encode(array('error'=> true, 'msg'=> $e->getMessage()));
}
}
ob_flush();
?>
<强> ajax.js 强>
// ajax signin
jQuery(document).ready(function() {
jQuery("#registertologin").click(function() {
jQuery("#loginreturn").html("<img src='img/loading.gif'/>");
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax( {
url : 'login.php',
type: "POST",
data : $('#loginform').serialize(),
dataType:"json",
success:function(data, textStatus, jqXHR) {
jQuery("#loginreturn").html('<pre><code class="returndata">'+data+'</code></pre>');
$("#loginform").submit_login(); //SUBMIT FORM
if(data.error)
{
console.log('data.msg');
}
else
{
window.location.href = 'myaccount.php';
}
},
error: function(jqXHR, textStatus, errorThrown)
{
$("#loginreturn").html('<pre><code class="returndata">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
}
});
e.preventDefault(); //STOP default action
});
});