js:使用ajax获取数据库数据(后端采用php)

前端ajax部分

AllUnsigned.php

<script>
    //刚进入页面就执行
    $(document).ready(function() {
        // 发送AJAX请求
        .ajax({
             type: 'POST',
             url: 'get_allunsign.php',//请求的页面
              data: {//传递的参数
                  action: 'noread_info',
                  type1: type1,
                  order_number1: order_number1,
                  userinfo: userinfo
              },
              dataType: 'json',
              success: function(response) {//成功请求后执行的方法
                  if (!response) {
                      alert('数据不存在');
                  } else {
                      alert('数据成功返回');
                  }
              },
              error: function(jqXHR, textStatus, errorThrown) {
                  console.error(textStatus, errorThrown);
              },
        });
    });
</script>

数据库查询部分

get_allunsign.php

参数引用:$_POST['action']

返回数据:echo json_encode($data);

<?php
//引入连接数据库部分
require_once 'get_db_conn.php';
$conn = db_connect();
//如果传递的参数action等于noread_info,就执行下面的操作
if ($_POST['action'] == 'noread_info') {
    //查询表all_unsigned中status等于未读,并且info_user等于参数userinfo
    $sql1 = "select * from all_unsigned where status = '未读' and info_user = '" . $_POST['userinfo'] . "'";
    //连接模糊查询
    if (isset($_POST['type1']) and $_POST['type1'] != '') {
        $sql1 .= " and type LIKE '%" . $_POST['type1'] . "%' ";
    }
    if (isset($_POST['order_number1']) and $_POST['order_number1'] != '') {
        $sql1 .= " and order_number LIKE '%" . $_POST['order_number1'] . "%' ";
    }
    //连接顺序查询
    $sql1 .= " order by  creation_date desc ";
    //执行数据库
    $result1 = mysqli_query($conn, $sql1);
    // 检查结果集是否存在
    if (mysqli_num_rows($result1) > 0) {
        //如果存在数据就放入$data变量
        $data = [];
        while ($row = mysqli_fetch_assoc($result1)) {
            $data[] = $row;
        }
    //否则变量data等于0
    } else {
        $data = 0;
    }
    //输出返回值给请求的页面
    echo json_encode($data);
}

相关推荐

  1. js:使用ajax获取数据库数据采用php

    2024-03-27 11:34:05       40 阅读
  2. “探索AJAX:前端与数据交互的利器“

    2024-03-27 11:34:05       47 阅读
  3. vuews从ajax获取数据

    2024-03-27 11:34:05       53 阅读
  4. php 获取网页数据

    2024-03-27 11:34:05       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 11:34:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 11:34:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 11:34:05       82 阅读
  4. Python语言-面向对象

    2024-03-27 11:34:05       91 阅读

热门阅读

  1. Spring与Spring Boot:理解它们的区别与适用场景

    2024-03-27 11:34:05       44 阅读
  2. Spring 日志规范

    2024-03-27 11:34:05       39 阅读
  3. 查看k8s中的secret

    2024-03-27 11:34:05       40 阅读
  4. react native hooks 页面出现重绘问题,如何解决

    2024-03-27 11:34:05       34 阅读
  5. k8s kubeadm单机器安装

    2024-03-27 11:34:05       38 阅读
  6. npm发包常用指令

    2024-03-27 11:34:05       38 阅读