gpt4 book ai didi

php + populate 下拉菜单选择另一个

转载 作者:行者123 更新时间:2023-11-29 00:34:06 26 4
gpt4 key购买 nike

我正在创建三个下拉菜单,它工作得很好,但我希望第二个下拉列表出现在第一个的选择上,第三个出现在第二个的选择上如何如果有人可以指导我或给我一个例子,我将不胜感激

PS:第二个下拉列表或表有一个来自第一个的外键,所以在这里我想根据第一个的选择来填充第二个。

fun.inc.php

<?php
require_once('db.inc.php');

function connect(){
mysql_connect(DB_Host, DB_User ,DB_Pass )or die("could not connect to the database" .mysql_error());

mysql_select_db(DB_Name)or die("could not select database");

}
function close(){

mysql_close();

}

function countryQuery(){

$countryData = mysql_query("SELECT * FROM country");

while($record = mysql_fetch_array($countryData)){

echo'<option value="' . $record['country_name'] . '">' . $record['country_name'] . '</option>';

}

}

function specializationQuery(){

$specData = mysql_query("SELECT * FROM specialization");

while($recordJob = mysql_fetch_array($specData)){

echo'<option value="' . $recordJob['specialization_name'] . '">' . $recordJob['specialization_name'] . '</option>';

}


}

function governorateQuery(){

$goverData = mysql_query("SELECT * FROM governorate");

while($recordGover = mysql_fetch_array($goverData)){

echo'<option value="' . $recordGover['governorate_name'] . '">' . $recordGover['governorate_name'] . '</option>';

}


}

?>

索引.php

<?php
require_once('func.inc.php');
connect();


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>testDroplistdown</title>
</head>

<body>
<p align="center">
<select name="dropdown">
<?php countryQuery(); ?>
</select>
</p>
<br />
<br />

<p align="center">
<select name="dropdown2">
<?php governorateQuery(); ?>
</select>
</p>

<p align="left">
<select name="dropdown3">
<?php specializationQuery(); ?>
</select>
<?php close(); ?>
</p>


</body>
</html>

最佳答案

确保你永远不会在你的 php 结束标记和你的 html header 的请求之后留下 a,它可能会抛出一些讨厌的错误

这个脚本应该可以工作

   <?php
require_once('func.inc.php');
connect();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>testDroplistdown</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>

<body>
<p align="center">
<div id="dropdown1div"><select id="dropdown1" name="dropdown">
<?php countryQuery(); ?>
</select></div>
</p>
<br />
<br />

<p align="center">
<div id="dropdown2div"></div>
</p>

<p align="left">
<div id="dropdown3div"></div>

<script type="text/javascript">
$("#dropdown").change(function() {
val = $(this).val();
var html = $.ajax({
url: "dropdown_select.php?dropdown=2&val="+val+"",
async: true,
success: function(data) {
$('#dropdown2div').html(data);
}////////////function html////////
})/////////function ajax//////////
});
</script>

<?php close(); ?>
</p>


</body>
</html>

下拉选择.php

    <?php
require_once('func.inc.php');
connect();
if(isset($_GET['val'])){
$val = $_GET['val'];
$dropdown = $_GET['dropdown'];
}


if($dropdown == '2'){
echo '<select id="dropdown2" name="dropdown2">';
governorateQuery();
echo '</select>';
?>
<script type="text/javascript">
$("#dropdown2").change(function() {
val = $(this).val();
var html = $.ajax({
url: "dropdown_select.php?dropdown=3&val="+val+"",
async: true,
success: function(data) {
$('#dropdown3div').html(data);
}////////////function html////////
})/////////function ajax//////////
});
</script>

} // end if statement



if($dropdown == '3'){
echo '<select id="dropdown3" name="dropdown3">';
specializationQuery();
echo '</select>';

} // end if statement
close();
?>

关于php + populate 下拉菜单选择另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com