填充下拉选择下拉

时间:2017-10-17 12:59:07

标签: php jquery ajax drop-down-menu

如何在选择多个下拉列表时填充下拉列表值。例如,我有以下下拉列表。 enter image description here 在选择第二个下拉列表时,我想填写第三个下拉单选择我该怎么办。 ? 我目前的代码如下。

  //CALL FOR SECOND DROPDOWN 
    $(document).ready(function(){
    $('#select4').on('change',function(){
        var subcatgoryId = $(this).val();
        console.log(subcatgoryId);
        if(subcatgoryId){
            $.ajax({
                type:'POST',
                url:'ajax_load_specification.php',
                data:{subcatgoryId: subcatgoryId},
                success:function(html){
                  alert(html);
                  //$('#select5').html(html);
                  //$('#loading1').css("display","none")
                },
                error: function (jqXHR, exception) {
                  alert("Got Some Errore");
                }
            }); 
        }else{
            $('#select5').html('<option value="">Select Category first</option>');
        }
    });
  });

和php代码如下

if(isset($_POST["subcatgoryId"]) )
{
    $subcategory = explode(',', $_POST["subcatgoryId"]);

    print_r($_POST["subcatgoryId"]);
    foreach ($subcategory as $key => $value) 
    { 
        echo $key;
        echo "<br>";
        $query1 = "SELECT * FROM m_subcategory WHERE id = ".$item." ";
        $query1 = $conn->query($query1);
        $query1 = $query1->fetch_object();

        if($query1){
            $id = $query1->id;
            $name = $query1->name;
            echo '<option value="'.$id.'">'.$name.'</option>';  
        }else{
            echo '<option value="">We Get Empty Category</option>'; 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需使用For循环并开始工作

if(isset($_POST["subcatgoryId"]) )
{
    $subcategory  = $_POST["subcatgoryId"];
    $len=count($subcategory);

    for ($i=0; $i < $len; $i++) { 

        $query1 = "SELECT * FROM m_subcategory WHERE id = ".$subcategory[$i]." ";
        $query1 = $conn->query($query1);
        $query1 = $query1->fetch_object();

        if($query1){
            $id = $query1->id;
            $name = $query1->name;
            echo '<option value="'.$id.'">'.$name.'</option>';
        }else{
            echo '<option value="">We Get Empty Category</option>'; 
        }
    }

}