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);
}
相关推荐
dy17171 小时前
element-plus表格默认展开有子的数据
前端·javascript·vue.js
睡觉的时候不会困4 小时前
Redis 主从复制详解:原理、配置与主从切换实战
数据库·redis·bootstrap
程序员的世界你不懂5 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
自学也学好编程5 小时前
【数据库】Redis详解:内存数据库与缓存之王
数据库·redis
JAVA不会写6 小时前
在Mybatis plus中如何使用自定义Sql
数据库·sql
IT 小阿姨(数据库)6 小时前
PgSQL监控死元组和自动清理状态的SQL语句执行报错ERROR: division by zero原因分析和解决方法
linux·运维·数据库·sql·postgresql·centos
一朵梨花压海棠go6 小时前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript
ChinaRainbowSea6 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
一只小风华~7 小时前
Vue: Class 与 Style 绑定
前端·javascript·vue.js·typescript·前端框架
小马学嵌入式~8 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite