【问题标题】:Set the option available for select based on another select php根据另一个选择 php 设置可用于选择的选项
【发布时间】:2015-01-16 10:15:01
【问题描述】:

大陆表

  1. 亚洲
  2. 南美洲

国家表

  1. 日本
  2. 中国
  3. 巴西
  4. 秘鲁

这是我表中的数据,我从大陆表和国家表中获取所有数据,如下所示

    <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>

我想知道如何做到这一点,当我选择大陆(例如亚洲)时,可用于县的选项将是亚洲唯一的国家,即日本和中国。在此之后我还有另一张桌子,它是城市,但如果我在这里得到逻辑,我可以将它应用到城市。任何想法表示赞赏

“更新”

发现了一些东西here

我不熟悉的一个是 ajax 任何人都可以尝试解释它是如何工作的我想知道如何将 select 的 id 从这个 javascript sn-p 传递到我的 php 函数页面这里是 sn- p..

$('.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);
            }
        });

    });

在我的函数中的 php

function Country(){
if(isset($_POST['id'])){
echo $_POST['id'];
}
}

问题是 id 的值没有传递我如何将它传递给 function.php 我有点不明白。是不是发生在success:function(data){ 下一步我该怎么办?

FYI

警报没问题,它已经获得了大陆的 id,只是没有传递给 function.php 并且 console.log 什么也不写 当我将 url 更改为主页时,console.log 中选择所在的页面返回主页代码。

【问题讨论】:

  • 你需要 javascript。
  • 有没有办法不使用javascript?我对java脚本不是很好
  • 如果您想即时过滤,您别无选择。或者您需要使用多个页面,并且只打开一个选择框。提交页面,在下一页上,您可以拥有国家(并在 PHP 中过滤)。请注意,这对您的用户没有好处(这种工作方式对用户体验非常不利)。
  • @Blaatpraat 你能举个例子吗?

标签: php select foreach


【解决方案1】:

就像上面所说的,你可以使用 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

请记住,这些是谷歌搜索的第一个结果,因此,下次,尝试在堆栈溢出本身内搜索以避免重复问题。

【讨论】:

  • 我在阅读 ajax 教程后一直在搜索它,问题是他们并没有真正给出我理解的解释
猜你喜欢
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 2020-07-12
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多