使用 PHP 和 MySQL 填充基于 Jquery 的下拉列表

问题描述 投票:0回答:1

我有一个国家/州/城市级联下拉菜单。 数据使用PHP/Mysql填充。 数据是针对与国家对应的州/城市选项填写的。 数据也根据用户输入的数据进行填充以供其记录。 有两个问题。

  1. 当用户输入的数据输入下拉列表时,加载的数据会重复(就像他输入美国一样,又显示一个美国,因为它来自另一个查询来填充值)这里我们可以使用“selected”,但是当用户输入“selected”时如何做到这一点数据来自数据库。

  2. 第二个问题是级联不显示相应的条目 - 如果用户输入美国,则美国的州和城市不会显示在级联中,尽管显示了用户输入的任何内容。

这就是景色

Country:
<select class="addpropertyinput" name="property_country" id="country" > 
        <option value="<?php echo $data['property_country']; ?>">
        <?php   
        $country_id = $data['property_country'];
        $select = $con->prepare("SELECT country_name from tbl_country where country_id='$country_id'");
        $select->setFetchMode(PDO::FETCH_ASSOC);
        $select->execute();
        $data2 = $select->fetchAll();
        foreach ($data2 as $row1) {
        echo $row1['country_name'];
        }
        ?>
        </option>

        <?php foreach ($data1 as $row) { ?>
        <option value = "<?php echo $row['country_id']?>"><?php echo $row['country_name']?></option>
        <?php } ?>

    </select>

</div>


<div>


State:
<select class="addpropertyinput" name="property_state" id="state" > 
        <option value="<?php echo $data['property_state']; ?>"><?php echo $data['property_state']; ?></option>
    </select>

</div>



<div>


City :
    <select class="addpropertyinput" name="property_city" id="city"> 
        <option value=""><?php echo $data['property_city']; ?></option>
    </select>

</div>

这是Jquery

// Country State City Dropdown

    $('#country').on('change', function() {

        var country_id = $('#country').val();

        if (country_id == "")
            {
                $('#state').html('<option value="">State</option>');    
                $("#city").html(' <option value="">City</option>'); 

            }

        else {

        $.ajax({
                type: 'POST',
                url: '../classes/countrystatecity.php',
                data: "country_id="+country_id,
                success: function(msg) {

                $("#state").html(msg);  
                $("#city").html(' <option value="">City</option>'); 

                }
                });
    }

    });

    $('#state').on('change', function() {

        var state_id = $('#state').val();

        if (state_id == "")
            {
                $("#city").html(' <option value="">City</option>'); 

            }

        else {

    $.ajax({
            type: 'POST',
            url: '../classes/countrystatecity.php',
            data: "state_id="+state_id,
            success: function(msg) {

            $("#city").html(msg);               

            }
        }); 
    }
    });

这是模型:

$select = $con->prepare('SELECT country_id, country_name from tbl_country');
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
$data1 = $select->fetchAll();

// Select State

if (!empty($_POST['country_id'])){

        $country = $_POST['country_id'];
        $select = $con->prepare("SELECT state_id, state_name from tbl_state where country_id='$country'");
        $select->setFetchMode(PDO::FETCH_ASSOC);
        $select->execute();
        $data1 = $select->fetchAll();
        ?> 
        <option value="">State</option> 
        <?php
        foreach ($data1 as $row){ ?>
        <option value = "<?php echo $row["state_id"];?>"><?php echo $row["state_name"];?></option>
<?php   
         }
}

// Select City

if (!empty($_POST['state_id'])){

        $state = $_POST['state_id'];
        $select = $con->prepare("SELECT city_id, city_name from tbl_city where state_id='$state'");
        $select->setFetchMode(PDO::FETCH_ASSOC);
        $select->execute();
        $data1 = $select->fetchAll();
        ?> 
        <option value="">City</option> <?php
        foreach ($data1 as $row){ ?>
        <option value = "<?php echo $row["city_id"];?>"><?php echo $row["city_name"];?></option>
<?php   
         }
}
php dropdown
1个回答
2
投票

问题1的解决方案:

    Country:
    <select class="addpropertyinput" name="property_country" id="country" >
        <?php foreach ($data1 as $row) { 
            $selected = '';    
            if($row['country_id'] == $data['property_country']){
                $selected = 'selected';
            } ?>
            <option value = "<?php echo $row['country_id']?>" <?php echo $selected?>><?php echo $row['country_name']?></option>
        <?php } ?>
    </select>

问题2的解决方案:

    $select = $con->prepare('SELECT country_id, country_name from tbl_country');
    $select->setFetchMode(PDO::FETCH_ASSOC);
    $select->execute();
    $data1 = $select->fetchAll();
    
    // Select State
    
    if (!empty($_POST['country_id'])){
    
            $country = $_POST['country_id'];
            $select = $con->prepare("SELECT state_id, state_name from tbl_state where country_id='$country'");
            $select->setFetchMode(PDO::FETCH_ASSOC);
            $select->execute();
            $data1 = $select->fetchAll();
            $result = "<option value="">State</option>";
            foreach ($data1 as $row){ 
                $result .= "<option value=".$row["state_id"].">".$row["state_name"]."</option>";
            }
            echo $result;
    }
    
    // Select City
    
    if (!empty($_POST['state_id'])){
    
            $state = $_POST['state_id'];
            $select = $con->prepare("SELECT city_id, city_name from tbl_city where state_id='$state'");
            $select->setFetchMode(PDO::FETCH_ASSOC);
            $select->execute();
            $data1 = $select->fetchAll();
            $result = "<option value="">City</option>";
            foreach ($data1 as $row){ 
                $result .= "<option value=".$row["city_id"].">".$row["city_name"]."</option>";
            }
            echo $result;
    }
© www.soinside.com 2019 - 2024. All rights reserved.