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);
}
相关推荐
技术小丁17 分钟前
使用 HTML + JavaScript 实现酒店订房日期选择器(附完整源码)
前端·javascript
hashiqimiya19 分钟前
harmonyos的鸿蒙的跳转页面的部署
开发语言·前端·javascript
万事大吉CC25 分钟前
SQL语法基础教程
数据库·oracle
闭着眼睛学算法36 分钟前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
Drift_Dream41 分钟前
深入浅出 requestAnimationFrame:让动画更流畅的利器
javascript
betazhou42 分钟前
Oracle dgbroker常规命令管理简介
数据库·oracle·adg·dbbroker
GIS瞧葩菜1 小时前
【无标题】
开发语言·前端·javascript·cesium
彭于晏爱编程1 小时前
关于表单,别做工具库舔狗
前端·javascript·面试
拉不动的猪1 小时前
什么是二义性,实际项目中又有哪些应用
前端·javascript·面试
烟袅1 小时前
LeetCode 142:环形链表 II —— 快慢指针定位环的起点(JavaScript)
前端·javascript·算法