当前位置:Gxlcms > 数据库问题 > php mysql jquery ajax 查询数据库三级联动

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

时间:2021-07-01 10:21:17 帮助过:14人阅读

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" onchange="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" onchange="getModulePage()">
<option value="c">--请选择--</option>
</select>
<select id="modulePage">
<option value="c">--请选择--</option>
</select>
</body>

 

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

标签:null   primary   代码   联动   htm   res   cal   module   change   

人气教程排行