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

前端ajax部分

AllUnsigned.php

html 复制代码
<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 复制代码
<?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);
}
相关推荐
Moment1 小时前
从方案到原理,带你从零到一实现一个 前端白屏 检测的 SDK ☺️☺️☺️
前端·javascript·面试
七七知享2 小时前
Go 语言编程全解析:Web 微服务与数据库十大专题深度精讲
数据库·web安全·网络安全·微服务·golang·web3·webkit
野生的程序媛2 小时前
重生之我在学Vue--第5天 Vue 3 路由管理(Vue Router)
前端·javascript·vue.js
鱼樱前端2 小时前
Vue 2 与 Vue 3 响应式原理详细对比
javascript·vue.js
codingandsleeping2 小时前
前端工程化之模块化
前端·javascript
CodeCraft Studio2 小时前
报表控件stimulsoft操作:使用 Angular 应用程序的报告查看器组件
前端·javascript·angular.js
Faith_xzc2 小时前
Doris vs ClickHouse 企业级实时分析引擎怎么选?
大数据·数据库·clickhouse·数据库开发
m0_748235613 小时前
MySQL 实战 4 种将数据同步到ES方案
数据库·mysql·elasticsearch
曼诺尔雷迪亚兹3 小时前
2025年四川烟草工业计算机岗位备考详细内容
数据结构·数据库·计算机网络·算法
Liigo3 小时前
初次体验Tauri和Sycamore(3)通道实现
javascript·rust·electron·tauri·channel·sycamore