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);
}
相关推荐
rchmin5 分钟前
向量数据库Milvus安装及使用实战经验分享
数据库·milvus
吴声子夜歌8 分钟前
ES6——Generator函数详解
前端·javascript·es6
吴声子夜歌10 分钟前
ES6——Set和Map详解
前端·javascript·es6
ego.iblacat11 分钟前
Python 连接 MySQL 数据库
数据库·python·mysql
祖传F8722 分钟前
quickbi数据集数据查询时间字段显示正确,仪表板不显示
数据库·sql·阿里云
Leon-Ning Liu42 分钟前
Oracle 26ai新特性:时区、表空间、审计方面的新特性
数据库·oracle
humors2211 小时前
各厂商工具包网址
java·数据库·python·华为·sdk·苹果·工具包
xinzheng新政1 小时前
Javascript 深入学习基础·4
javascript·学习·servlet
粥里有勺糖1 小时前
视野修炼-技术周刊第129期 | 上一次古法编程是什么时候
前端·javascript·github
whuhewei2 小时前
JS获取CSS动画的旋转角度
前端·javascript·css