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);
}
相关推荐
ss27311 分钟前
AI时代首先数据库-PostgreSQL实战笔记 | 11 PostgreSQL安装部署常见问题排查与解决
数据库·笔记·postgresql
张张123y18 分钟前
# 后端第八课:数据库入门(给 FastAPI 接口加上“记忆“,存下问答历史)
数据库·sql·mysql·oracle·fastapi·sqllite
cspttty28 分钟前
2027秋招市场调研岗:统计学专业如何补齐业务、SQL与研究交付能力
大数据·数据库
BD_Marathon38 分钟前
Milvus数据库的DML操作
数据库·milvus
用户831348593069841 分钟前
Vue3 事件:常用用法与开发高频坑点
前端·javascript·vue.js
其实防守也摸鱼44 分钟前
存储管理:页面置换算法学习任务(测验题要求给出置换页面顺序,缺页次数,缺页率)
android·前端·数据库·学习·oracle·自动化
cspttty44 分钟前
2027校招:大数据管理与应用专业如何匹配电商运营与供应链管培生能力要求
大数据·数据库
ss2731 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 12 PostgreSQL数据目录与重要文件结构
数据库·笔记·postgresql
漫步人生走在路上1 小时前
用小语种SEO突破外贸B2B天花板:法语、德语市场的实测数据
开发语言·前端·javascript·人工智能·搜索引擎·ecmascript
变与不变8061 小时前
jQuery 核心语法与 jQuery AJAX
前端·ajax·jquery