简体   繁体   English

设置可用于基于另一个选择php的选项

[英]Set the option available for select based on another select php

Continent Table 大陆表

  1. ASIA 亚洲
  2. SOUTH AMERICA 南美洲

Country Table 国家表

  1. Japan 日本
  2. China 中国
  3. Brazil 巴西
  4. Peru 秘鲁

This is the data in my table and i get all the data from continent table and country table like this 这是我表中的数据,我从像这样的大洲表和国家表中获取所有数据

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

This is for country 这是给国家的

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

I want to know how to make it in a way that when i select the continent for example asia the option available for county will be the country in asia only which is japan and china. 我想知道如何以某种方式做到这一点,例如当我选择亚洲大陆时,可用于县的选项将是仅亚洲的国家,即日本和中国。 I also have another table after this which is city but if i get the logic here i can apply it to city. 在这之后我还有另一个表是city,但是如果我在这里有逻辑,我可以将其应用于city。 Any idea is appreciated 任何想法表示赞赏

"UPDATE" “更新”

Found something here 在这里找到了东西

One that i am not familiar with is the ajax can anybody try to explain how it work i want to know how i can pass the id of the select from this javascript snippet to my php function page here is the snippet.. 我不熟悉的一个是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);
            }
        });

    });

in my function the php 在我的功能的PHP

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

The problem is the value of id is not passed how do i pass it onto the function.php i kind of out of idea here. 问题是没有传递id的值,我该如何将其传递给function.php呢? Does it happened in success:function(data){ what do i do next? success:function(data){发生了吗success:function(data){我接下来要做什么?

FYI

The alert is ok it gets the id of the continent already it is just not passed to the function.php and console.log writes nothing and when i change the url to main page the page where the select are in the console.log returns the main page code. 警报是确定的,它已经获取了大陆的ID,它只是没有传递给function.php和console.log,什么也不写,当我将URL更改为主页时,console.log中选择所在的页面将返回主页代码。

Like it was said, you can use ajax. 就像有人说的那样,您可以使用ajax。 There is a static non-ajax way of doing it, but this way it is better in the long run. 有一种静态的非ajax方式,但是从长远来看,这种方式会更好。

Basically, what you do with that jQuery is listen for a change in the continent select box, and when a change happens, you make a request to the server asking: "give me all the countries within this continent". 基本上,您使用jQuery所做的就是侦听大洲选择框中的更改,当发生更改时,您向服务器发出请求,询问:“给我该大洲内的所有国家”。 You should have a database for this or you can map them in static objects just to test it out. 您应该为此拥有一个数据库,或者可以将它们映射到静态对象中以进行测试。

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

This would of course make you add the countries_container in the HTML like and inside it you would render the select : 这当然会令你添加countries_container像HTML和它里面你会渲染select

<div class="countries_container"></div>

Now in the actual PHP code server side (getCountries) you could do something like: 现在,在实际的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
}

This code can most defiantly be improved, but I tried to explain it in a understandable way. 可以最大程度地改进此代码,但是我尝试以一种易于理解的方式对其进行解释。

Also, you should check: 另外,您应该检查:

Jquery ajax populate dropdown with json response data jQuery ajax使用json响应数据填充下拉列表

Demo of linked drop down list by using Ajax & PHP 使用Ajax和PHP的链接下拉列表演示

Keep on mind, these were the first results from a google search therefore, next time, try to search within stack overflow itself to avoid duplicate questions. 请记住,这些是google搜索的第一个结果,因此,下次尝试在堆栈溢出本身中进行搜索,以避免重复的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM