PHP函数在Ajax调用上返回null

时间:2015-12-20 08:55:14

标签: php jquery ajax twitter-bootstrap

在bootstrap 3.0中单击选项卡时,我有一个Ajax调用

以下是JS代码

$('[data-toggle="tab"]').click(function (e) {   
   var $this = $(this);
   loadtab = $this.attr('href');
   if(loadtab=="#odhis"){
        var email = $("#email");
        $.ajax({
              type: "POST",
              url: "../orders.php",
              data: {'email':email.val()},
              dataType: "json",
              success: function(response){
               console.log(response);
              },
              error: function(xhr, status, error){
               console.log(xhr.responseText);
              }
       });
    }
});

PHP文件orders.php

function getCustomOrderHistory(array $data){
    $cstm = array();
    if( !empty( $data ) ){  
        $trimmed_data = array_map('trim', $data);
        $mail = mysqli_real_escape_string( $this->_con,  $trimmed_data['email'] );
        $checktable = "SELECT * from `orders` WHERE csmail='$mail'";
        $resultcstm = mysqli_query($this->_con, $checktable);
        while($row = mysqli_fetch_array($resultcstm)){
            $cstm[] = $row;
        }
    }
    return $cstm;
}

现在,当我print_r($cstm)时,它会在PHP文件中打印结果。 但在JS中,成功响应是空的。

请帮忙吗?

1 个答案:

答案 0 :(得分:2)

由于您的dataType: "json"而不是return只是对$cstm数组和echo进行编码,如下所示:

function getCustomOrderHistory(array $data){
    $cstm = array();
    if( !empty( $data ) ){  
        $trimmed_data = array_map('trim', $data);
        $mail = mysqli_real_escape_string( $this->_con,  $trimmed_data['email'] );
        $checktable = "SELECT * from `orders` WHERE csmail='$mail'";
        $resultcstm = mysqli_query($this->_con, $checktable);
        while($row = mysqli_fetch_array($resultcstm)){
            $cstm[] = $row;
        }
    }
    $json = json_encode($cstm);
    echo $json;
}