前面AJAX及后端PHP处理的代码,其中包含了处理JSON数据的方法、加载样式的loading效果、防止重复操作的禁用按钮功能。
function showshibie(){ zc_code=$("#zc_code").val() if (zc_code==''){ alert_go('请输入资产编号!','alert','','error','') return false }else{ $('#but11').attr("disabled","disabled"); $('#but11').addClass("disabled"); $("#cz_tishi").html('<span><img src="images/16.gif" border="0" /> 请稍后,正在读取...</span>'); setTimeout(function(){ $.ajax({ type: "POST", timeout:10000, // 设置超时时间10秒 url: "weixiu_function.php", contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型 dataType: "json", //表示返回值类型,不必须。如果注释掉表示字符型 data: JSON.stringify({ "action": 'get', "zc_code": zc_code }), success: function(jsonResult) { var back_status = jsonResult.back_status; if (back_status == 'error') { //错误 var back_message = jsonResult.back_message; $("#cz_tishi").html(back_message) $("#cz_tishi").css("color","#FF0000"); $('#but11').removeAttr("disabled"); $('#but11').removeClass("disabled"); return false; } else if (back_status == 'ok') { //正常 var zc_code = jsonResult.zc_code; var pinming = jsonResult.pinming; var guige = jsonResult.guige; $("#zc_bianhao").val(zc_code); $("#pingming").html(pinming); $("#guige").html(guige); $("#cz_tishi").html(''); $("#cz_tishi").css("color","green"); $('#but11').removeAttr("disabled"); $('#but11').removeClass("disabled"); } }, error: function(data) { $("#cz_tishi").html('error') $("#cz_tishi").css("color","#FF0000"); $('#but11').removeAttr("disabled"); $('#but11').removeClass("disabled"); return false; } }); return false },400); } }
PHP后台处理代码:
<?php $post_val=file_get_contents('php://input');//jq ajax的json数据用此方法接收 $post_str=json_decode($post_val, true); $action=$post_str['action']; $zc_code=$post_str['zc_code']; if ($action=='get'){ if (strpos($zc_code,"-")===false){ echo '{"back_status":"error","back_message":"资产编码格式错误,请完整输入!"}'; exit; } $biaoji=explode('-',$zc_code); if ($biaoji[0]=='FGZ'){ $select_table='dangxiao_zichan_guzi_1'; $select_pm_table='dangxiao_zichan_fenlei'; }elseif ($biaoji[0]=='GZ'){ $select_table='dangxiao_zichan_guzi_2'; $select_pm_table='dangxiao_zichan_fenlei_guzi'; } if ($biaoji[0]=='GZ' || $biaoji[0]=='FGZ' ){ $sql_2="select id,pinming,guige from ".$select_table." where zhangtao='".$_SESSION['dangxiao_zc_zhangtao']."' and id='".$biaoji[1]."' limit 1 "; $result_2=$mysqli->query($sql_2); if ($rs_2=$result_2->fetch_assoc()){ $guige=$rs_2["guige"]; $id=$rs_2["id"]; $pinming=$rs_2["pinming"]; $sql_1="select biaoqian from ".$select_pm_table." where zhangtao='".$_SESSION['dangxiao_zc_zhangtao']."' and id='".$pinming."' limit 1 "; $result_1=$mysqli->query($sql_1); if ($rs_1=$result_1->fetch_assoc()){ $biaoqian=$rs_1["biaoqian"]; } echo '{"back_status":"ok","zc_code":"'.$zc_code.'","pinming":"'.$biaoqian.'","guige":"'.$guige.'"}';//因为要往每行写内容,所以要返回变量而非message内容 exit; }else{ echo '{"back_status":"error","back_message":"资产编号【'.$zc_code.'】不存在,请核对!"}'; exit; } } if ($biaoji[0]=='QT'){ $sql_2="select id,guige,danwei_wz,biaoqian from dangxiao_zichan_fenlei_qita where zhangtao='".$_SESSION['dangxiao_zc_zhangtao']."' and id='".$biaoji[1]."' limit 1 "; $result_2=$mysqli->query($sql_2); if ($rs_2=$result_2->fetch_assoc()){ $guige=$rs_2["guige"]; $id=$rs_2["id"]; $biaoqian=$rs_2["biaoqian"]; echo '{"back_status":"ok","zc_code":"'.$zc_code.'","pinming":"'.$biaoqian.'","guige":"'.$guige.'"}';//因为要往每行写内容,所以要返回变量而非message内容 exit; }else{ echo '{"back_status":"error","back_message":"资产编号【'.$zc_code.'】不存在,请核对!"}'; exit; } } } ?>