AJAX+PHP前后端实用代码示例

后端开发   发布日期:2023年05月31日   浏览次数:511

前面AJAX及后端PHP处理的代码,其中包含了处理JSON数据的方法、加载样式的loading效果、防止重复操作的禁用按钮功能。

  1. function showshibie(){
  2. zc_code=$("#zc_code").val()
  3. if (zc_code==''){
  4. alert_go('请输入资产编号!','alert','','error','')
  5. return false
  6. }else{
  7. $('#but11').attr("disabled","disabled");
  8. $('#but11').addClass("disabled");
  9. $("#cz_tishi").html('<span><img src="images/16.gif" border="0" /> 请稍后,正在读取...</span>');
  10. setTimeout(function(){
  11. $.ajax({
  12. type: "POST",
  13. timeout:10000, // 设置超时时间10秒
  14. url: "weixiu_function.php",
  15. contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型
  16. dataType: "json", //表示返回值类型,不必须。如果注释掉表示字符型
  17. data: JSON.stringify({
  18. "action": 'get',
  19. "zc_code": zc_code
  20. }),
  21. success: function(jsonResult) {
  22. var back_status = jsonResult.back_status;
  23. if (back_status == 'error') { //错误
  24. var back_message = jsonResult.back_message;
  25. $("#cz_tishi").html(back_message)
  26. $("#cz_tishi").css("color","#FF0000");
  27. $('#but11').removeAttr("disabled");
  28. $('#but11').removeClass("disabled");
  29. return false;
  30. } else if (back_status == 'ok') { //正常
  31. var zc_code = jsonResult.zc_code;
  32. var pinming = jsonResult.pinming;
  33. var guige = jsonResult.guige;
  34. $("#zc_bianhao").val(zc_code);
  35. $("#pingming").html(pinming);
  36. $("#guige").html(guige);
  37. $("#cz_tishi").html('');
  38. $("#cz_tishi").css("color","green");
  39. $('#but11').removeAttr("disabled");
  40. $('#but11').removeClass("disabled");
  41.  
  42. }
  43. },
  44. error: function(data) {
  45. $("#cz_tishi").html('error')
  46. $("#cz_tishi").css("color","#FF0000");
  47. $('#but11').removeAttr("disabled");
  48. $('#but11').removeClass("disabled");
  49. return false;
  50. }
  51. });
  52. return false
  53. },400);
  54. }
  55. }

PHP后台处理代码:

  1. <?php
  2.  
  3. $post_val=file_get_contents('php://input');//jq ajax的json数据用此方法接收
  4. $post_str=json_decode($post_val, true);
  5. $action=$post_str['action'];
  6. $zc_code=$post_str['zc_code'];
  7.  
  8. if ($action=='get'){
  9. if (strpos($zc_code,"-")===false){
  10. echo '{"back_status":"error","back_message":"资产编码格式错误,请完整输入!"}';
  11. exit;
  12. }
  13. $biaoji=explode('-',$zc_code);
  14. if ($biaoji[0]=='FGZ'){
  15. $select_table='dangxiao_zichan_guzi_1';
  16. $select_pm_table='dangxiao_zichan_fenlei';
  17. }elseif ($biaoji[0]=='GZ'){
  18. $select_table='dangxiao_zichan_guzi_2';
  19. $select_pm_table='dangxiao_zichan_fenlei_guzi';
  20. }
  21.  
  22. if ($biaoji[0]=='GZ' || $biaoji[0]=='FGZ' ){
  23. $sql_2="select id,pinming,guige from ".$select_table." where zhangtao='".$_SESSION['dangxiao_zc_zhangtao']."' and id='".$biaoji[1]."' limit 1 ";
  24. $result_2=$mysqli->query($sql_2);
  25. if ($rs_2=$result_2->fetch_assoc()){
  26. $guige=$rs_2["guige"];
  27. $id=$rs_2["id"];
  28. $pinming=$rs_2["pinming"];
  29. $sql_1="select biaoqian from ".$select_pm_table." where zhangtao='".$_SESSION['dangxiao_zc_zhangtao']."' and id='".$pinming."' limit 1 ";
  30. $result_1=$mysqli->query($sql_1);
  31. if ($rs_1=$result_1->fetch_assoc()){
  32. $biaoqian=$rs_1["biaoqian"];
  33. }
  34. echo '{"back_status":"ok","zc_code":"'.$zc_code.'","pinming":"'.$biaoqian.'","guige":"'.$guige.'"}';//因为要往每行写内容,所以要返回变量而非message内容
  35. exit;
  36. }else{
  37. echo '{"back_status":"error","back_message":"资产编号【'.$zc_code.'】不存在,请核对!"}';
  38. exit;
  39. }
  40. }
  41. if ($biaoji[0]=='QT'){
  42. $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 ";
  43. $result_2=$mysqli->query($sql_2);
  44. if ($rs_2=$result_2->fetch_assoc()){
  45. $guige=$rs_2["guige"];
  46. $id=$rs_2["id"];
  47. $biaoqian=$rs_2["biaoqian"];
  48. echo '{"back_status":"ok","zc_code":"'.$zc_code.'","pinming":"'.$biaoqian.'","guige":"'.$guige.'"}';//因为要往每行写内容,所以要返回变量而非message内容
  49. exit;
  50. }else{
  51. echo '{"back_status":"error","back_message":"资产编号【'.$zc_code.'】不存在,请核对!"}';
  52. exit;
  53. }
  54. }
  55. }
  56.  
  57. ?>


以上就是AJAX+PHP前后端实用代码示例的详细内容,更多关于AJAX+PHP前后端实用代码示例的资料请关注九品源码其它相关文章!