大陆表
>亚洲
>南美洲
国家表
>日本
>中国
>巴西
>秘鲁
这是我表格中的数据,我从大陆表和国家/地区表中获取所有数据
<select name="continent" class='required InputBox'>
<?PHP echo "<option value=''></option>";
foreach (Contintent() as $value) {
echo "<option id='" . $value[ 'sysid'] . "' value='" . $value[ 'name'] . "'>" . $value[ 'name'] . "</option>"; } ?>
</select>
这是为了国家
<select name="country" class='required InputBox'>
<?PHP echo "<option value=''></option>";
foreach (Country() as $value) {
echo "<option id='" . $value[ 'sysid'] . "' value='" . $value[ 'name'] . "'>" . $value[ 'name'] . "</option>"; } ?>
</select>
我想知道如何制作它,当我选择大陆例如亚洲时,县可用的选项将是仅在亚洲的国家,即日本和中国.在此之后我还有另一张桌子,这是城市,但如果我在这里得到逻辑,我可以将它应用到城市.任何想法都表示赞赏
“UPDATE”
发现了一些东西here
一个我不熟悉的是ajax可以任何人尝试解释它是如何工作的我想知道如何将这个javascript片段中的select的id传递给我的PHP函数页面这里是片段..
$('.continent').change(function() {
var id = $(this).find(':selected')[0].id;
//alert(id);
$.ajax({
type:'POST',
url:'../include/functions.PHP',
data:{'id':id},
success:function(data){
// the next thing you want to do
console.log(data);
alert(id);
}
});
});
function Country(){
if(isset($_POST['id'])){
echo $_POST['id'];
}
}
问题是id的值没有传递我如何将它传递给function.PHP我在这里有点想法.它是否成功发生:功能(数据){接下来我该怎么办?
FYI
警报是可以的,它获得了大陆的id已经没有传递给function.PHP
和console.log什么都不写
当我将url更改为主页面时,select.in在console.log中的页面返回主页面代码.
解决方法:
就像它说的那样,你可以使用ajax.
有一种静态的非ajax方式,但从长远来看,这种方式更好.
基本上,你使用jQuery做的是在大陆选择框中监听更改,当发生更改时,你向服务器发出请求:“给我这个大陆内的所有国家”.您应该有一个数据库,或者您可以将它们映射到静态对象中以便测试它.
$('.continent').change(function() {
var id = $(this).val(); //get the current value's option
$.ajax({
type:'POST',
url:'<path>/getCountries',
data:{'id':id},
success:function(data){
//in here, for simplicity, you can substitue the HTML for a brand new select Box for countries
//1.
$(".countries_container").html(data);
//2.
// iterate through objects and build HTML here
}
});
});
这当然会让你在HTML中添加countries_container,在里面你可以渲染select:
<div class="countries_container"></div>
现在在实际的PHP代码服务器端(getCountries),您可以执行以下操作:
$id = $_POST['id'];
if(isset($id) && <other validations>)){
// 1. your query to get countries from a given continent
// and for each result you can build the HTML itself
// 2. OR, a better solution is to return an Object and then in the
// success:function(data){ } iterate through it
}
这个代码最容易被改进,但我试图以一种可以理解的方式解释它.
另外,你应该检查:
Jquery ajax populate dropdown with json response data
Demo of linked drop down list by using Ajax & PHP
请记住,这些是Google搜索的第一个结果,因此,下次尝试在堆栈溢出内搜索以避免重复的问题.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。