0

我使用 wordpress 作为我的基础并制作了一个自定义登录表单。

阿贾克斯:

(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 :
    &nbsp;&nbsp;
    <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>

函数.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;
}

登录当前运行良好,但每当 $message 抛出错误时,它都会与标题警告一起显示。

警告:无法修改标头信息 - 标头已由 /%wordpresslocation%/wp-content/themes/%theme 中的(输出开始于 /%wordpresslocation%/wp-content/themes/%theme%/functions.php:77)发送第 86 行的 %/functions.php 空

回答 我觉得自己像个白痴,想通了,因为我精通 php,所以我一直在混合 php 和 javascript。

(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;

并将参数发回

function check_login( $params ){
4

1 回答 1

2

那是因为打印了一个错误(echo ucwords('请输入正确的用户详细信息');)

然后,您尝试设置标题。这是不可能的,标题总是必须在其他任何东西之前设置(这就是http的工作方式)

在发送标题之前,您必须重写打印文本的部分。它也不会发送有效的json,所以无论如何它都不会工作

于 2013-02-28T19:41:35.077 回答