日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

php mysql jquery ajax 查询数据库三级联动

發(fā)布時間:2024/6/30 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php mysql jquery ajax 查询数据库三级联动 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、php 頁面打開直接展示第一個select option中的數(shù)據(jù)

2、當?shù)谝粋€下拉列表選中的內(nèi)容發(fā)生改變的時候,查詢數(shù)據(jù)庫填充第二個下拉列表

3、當?shù)诙€下拉列表選中的內(nèi)容發(fā)生改變時,查詢數(shù)據(jù)庫填充第三個下拉列表

?

注意點:

1、查詢出來的數(shù)據(jù),如果綁定到select上

2、select cochange事件

3、ajax 請求,提交到某個php 頁面,參數(shù)是下拉列表選中的值,需要知道如何獲取,查詢數(shù)據(jù)庫有結(jié)果,如果將返回的json格式的數(shù)據(jù)進行解析

?

代碼:

CREATE TABLE `acl_action` (
? `action_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
? `parent_id` INT(10) UNSIGNED DEFAULT NULL,
? `action_name` VARCHAR(35) DEFAULT NULL,
? PRIMARY KEY (`action_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

?

connect.php 頁面

header("Content-Type:text/html;charset=utf-8");

$mysqli = new mysqli('localhost', 'root', '', 'c');
if ($mysqli->errno) {
die('Connect Error:' . $mysqli->error);
} else {
$mysqli->set_charset('UTF8');
}


ajax 提交頁面
demo04.php

require_once "connect.php";
require_once "function.php";

if(isset($_GET["module"])){
$module = $_GET["module"];
echo getModuleMessage($mysqli,$module);
}

if(isset($_GET["moduleChild"])){
$moduleChild=$_GET["moduleChild"];
echo getModuleChildMessage($mysqli,$moduleChild);
}

function.php 頁面
require_once "connect.php";

function getModuleMessage($mysqli,$module){
$result = $mysqli->query("SELECT action_id,action_name FROM acl_action where parent_id=" . $module);
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$select[] = array("action_id" => $row['action_id'], "action_name" => $row['action_name']);
}
return json_encode($select);
}
return 0;
}

function getModuleChildMessage($mysqli,$moduleChild){
$result = $mysqli->query("SELECT action_id,action_name FROM acl_action where parent_id=" . $moduleChild);
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$select[] = array("action_id" => $row['action_id'], "action_name" => $row['action_name']);
}
return json_encode($select);
}
}
demo04.js 頁面
function getModuleChild() {
$.getJSON("demo0402.php", {module: $("#module").val()}, function (json) {
if(json==0){
clearChild();
}
var moduleChild = $("#moduleChild");
$("option", moduleChild).not(":first").remove();
$.each(json, function (index, array) {
var option = "<option value='" + array['action_id'] + "'>" + array['action_name'] + "</option>";
moduleChild.append(option);
});
clearChild();
});
}

function clearChild(){
var modulePage = $("#modulePage");
$("option", modulePage).not(":first").remove();
}

function getModulePage() {
$.getJSON("demo0402.php", {moduleChild: $("#moduleChild").val()}, function (json) {
var modulePage = $("#modulePage");
$.each(json, function (index, array) {
var option = "<option value='" + array['action_id'] + "'>" + array['action_name'] + "</option>";
modulePage.append(option);
});
});
}

php 文件,里面嵌套html代碼

<script src="jquery-2.1.4.js"></script>
<script src="demo04.js"></script>
<body>
<select id="module" οnchange="getModuleChild()">
<option value="c">--請選擇--</option>
<?php
$result = $mysqli->query("SELECT action_id,action_name FROM acl_action where parent_id=0");
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) { ?>
<option value="<?php echo $row['action_id'] ?>"><?php echo $row['action_name'] ?></option>
<?php
}
}
?>
</select>
<select id="moduleChild" οnchange="getModulePage()">
<option value="c">--請選擇--</option>
</select>
<select id="modulePage">
<option value="c">--請選擇--</option>
</select>
</body>

?

轉(zhuǎn)載于:https://www.cnblogs.com/meroselove/p/7392532.html

總結(jié)

以上是生活随笔為你收集整理的php mysql jquery ajax 查询数据库三级联动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。