0
我使用wordpress作爲我的基礎並製作了自定義登錄表單。Ajax登錄返回修改標題錯誤
AJAX:
(function ($) {
jQuery(document).ready(function() {
$("#login_form").submit(function(event){
//check the username exists or not from ajax
jQuery.ajax({
type: 'POST',
url: my_ajax.ajaxurl,
data: $('#login_form').serialize(),
cache: false,
//dataType: 'json',
success: function(result) {
var result=trim(result);
if(result == 'success'){
window.location='/my-dashboard';
} else {
$('#message').html(result);
}
console.log(result);
}
});
return false;
});
});
function trim(str){
var str=str.replace(/^\s+|\s+$/,'');
return str;
}
}(jQuery));
登錄表單:
<p id="message" style="color:red;"></p>
<form method="post" action="" id="login_form">
<div align="center">
<div >
Email : <input name="email" type="text" id="email" value="" />
</div>
<div style="margin-top:5px" >
Password :
<input name="password" type="password" id="password" value="" />
</div>
<div class="buttondiv">
<input type="hidden" name="action" value="my_ajax_callback" />
<input type="hidden" name="func" value="check_login" />
<input name="submit" type="submit" id="submit" value="Login" style="margin-left:-10px; height:23px" /> <span id="msgbox" style="display:none"></span>
</div>
</div>
</form>
的functions.php
// Ajax
add_action('wp_ajax_nopriv_my_ajax_callback', 'my_ajax_callback');
add_action('wp_ajax_my_ajax_callback', 'my_ajax_callback');
// Your Login function
function check_login($params){
require_once('lib/hash.php');
$session = new SC_Session;
// now you can use $session here
$message=array();
if(isset($_POST['email']) && !empty($_POST['email'])){
mysqli_real_escape_string($mysqli, $params['email']);
}else{
$message[]='Please enter email';
}
if(isset($_POST['password']) && !empty($_POST['password'])){
$password= mysqli_real_escape_string($mysqli, $params['password']);
}else{
$message[]='Please enter password';
}
$countError=count($message);
if($countError > 0){
for($i=0;$i<$countError;$i++){
echo ucwords($message[$i]).'<br/><br/>';
}
} else {
$hc=$mysqli->query("SELECT password FROM %table% email='".$email."' AND active=1");
while($hp = $hc->fetch_object()) {
$stored_hash = $hp->password;
}
$hasherd = new PasswordHash(8, false);
$check = $hasherd->CheckPassword($password, $stored_hash);
if($check) {
//now validating the username and password
$result=$mysqli->query("SELECT id, first_name, last_name, zip, email, password FROM %table% WHERE email='".$email."' AND active=1");
while($row = $result->fetch_object()) {
//if username exists
if($result->num_rows > 0)
{
$date = date('Y-m-d h:i:s');
$update_sql = $mysqli->query("UPDATE %table% SET last_login='".$date."'");
$firstname = $row->first_name;
$lastname = $row->last_name;
$zip = $row->zip;
$user_id = $row->id;
$sex = $row->sex;
$session->set_userdata('user_id', $user_id);
$session->set_userdata('email', $email);
$session->set_userdata('firstname', $firstname);
$session->set_userdata('lastname', $lastname);
$session->set_userdata('zip', $zip);
$session->set_userdata('sex', $sex);
}
}
echo ucwords('success');
//return $params;
} else{
echo ucwords('please enter correct user details');
}
}
}
/**
* Ajax Submit
*/
function my_ajax_callback() {
$response = call_user_func($_POST['func'], $_POST);
header("Content-Type: application/json");
echo json_encode($response);
exit;
}
登錄目前的偉大工程,但每當一個錯誤是從$消息拋出它顯示以及標題警告。
警告:無法修改標頭信息 - /%wordpresslocation%/ wp-content /中已經發送的標頭(輸出開始於/%wordpresslocation%/wp-content/themes/%theme%/functions.php:77)上線86 空
ANSWER 我覺得自己像個白癡主題/%的主題%/ functions.php中,理解了它,我一直用JavaScript混合PHP作爲我在PHP精通。
(function ($) {
jQuery(document).ready(function() {
$('#message').slideUp();
$("#login_form").submit(function(event){
$('#message').slideUp();
//check the username exists or not from ajax
jQuery.ajax({
type: 'POST',
url: my_ajax.ajaxurl,
data: $('#login_form').serialize(),
dataType: 'json',
success: function(params) {
if(params == 'success'){
$('#message').html(params).fadeIn();
document.location='/my-dashboard';
} else {
$('#message').html(params).fadeIn();
}
}
});
return false;
});
});
}(jQuery));
和改變
echo ucwords('success');
//return $params;
} else{
echo ucwords('please enter correct user details');
}
此
$params = 'success';
return $params;
}else{
$params = 'fail';
return $params;
並且因爲印刷錯誤發回的PARAMS
function check_login($params){