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.