weixin_33725722 2018-06-18 10:44 采纳率: 0%
浏览 31

错误的ajax响应

I want to display an error message if the fetch array result from ajax page contains anything, and do nothing if the array result is empty. My jQuery is not returning correct value. Please help me to solve this. Here is my jQuery ajax code.

function getmsg(obj){
    //var invoc_num = $("#inv_num").val();
    //alert(invoc_num); 
    $.ajax({
        type: 'POST',
        url: 'check_invoc_num.php',
        data: "invoc_num="+ invoc_num,
        async: false,
        success: function(data) {
            //alert(data);
            var data = data.replace(/(^\s+|\s+$)/g,'');
            alert(data);
            if(data == "ok"){                                       
            }
            else{       
                $('#inv_msg').html("invoice number exists..!");     
            }                   
        } 
    });
}

My php code

if(isset($_SESSION['msg_agency_code'])){    
    $agency_code=$_SESSION['msg_agency_code'];
}

$invoc_num=$_POST['invoc_num'];

echo $CheckInvoicenum = $timesheetObj>CheckInvoicebyId($agency_code,$invoc_num);
if (empty($CheckInvoicenum)){
    echo "ok";
} else {
    echo "exists";
}

For every input for invoc_num i am getting ok as result.

  • 写回答

1条回答 默认 最新

  • weixin_33690963 2018-06-18 11:41
    关注

    Check this particular line from your php code

    echo $CheckInvoicenum = $timesheetObj>CheckInvoicebyId($agency_code,$invoc_num);

    It probably needs to be this.

    $CheckInvoicenum = $timesheetObj->CheckInvoicebyId($agency_code,$invoc_num);

    Also, your MySQL query is always going to return empty if no $agency_code value is passed (or is always empty) and no records in the database match this. You may want to use the $agency_code variable in the query, only if it is not empty.

    You may also want to ensure that your database call returns the coreect value. To verify this, take care of the following -

    a) Select only the columns that you need from the select query.
    b) in the return statement do return $result[0]; if you need the first column's value

    评论

报告相同问题?