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

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题