当第一个更改时,我无法加载第二个下拉菜单。第二次下拉取决于第一次

时间:2013-05-10 12:24:53

标签: php jquery mysql

这是我的代码,显示第二个值时出现问题:

HTML:我的表单,第一个下拉列表我从数据库中获取带有查询的元素。

                    <form name="farmer" action="index.php" method="post">
                                    <label>
                                        <span>Chose Land:</span>
                                        <select name="land" id="land">
                                            <option value="">--land--</option>
                                                <?php 
                                                    $sql="SELECT `city` FROM `lands`";
                                                    $result =mysql_query($sql);
                                                    while ($data=mysql_fetch_assoc($result)){
                                                ?>
                                            <option value ="<?php echo $data['city'] ?>" ><?php echo $data['city'] ?></option>
                                                <?php } ?>
                                        </select>
                                    </label>

                                <label>
                                        <span>Region:</span>
                                        <select name="region" id="region">
                                            <option value="">--region--</option>
                                        </select>
                                </label>

                     <input class="button4" type="submit" name="submit" value="Submit" />
                 </form>

JS

        jQuery(document).ready(function() {
        jQuery('#land').change(function() {
            jQuery.post(
                'getList.json.php', {
                    'land': jQuery('#land').val()
                },
                function(data, textStatus) {
                    jQuery('#region').empty();
                        if(data != null)
                        {
                            jQuery.each(data, function(index, value) {
                                jQuery('#region').append('<option value="' + value + '">' + value + '</option>');
                            });
                        }
                        else {
                            jQuery('#region').append('<option value="">Please select...</option>');
                        }
                },
                'json'
            );
        });
     });

getList.json.php文件 - 在这里,我使用query(JOIN)在region和land之间建立连接。

<?php

mysql_connect("localhost", "root", "") or die( "Unable to connect to database");
mysql_select_db("farmer_fields") or die( "Unable to select database");

if($_POST && $_POST['land'] != "") {

    $sql="SELECT region FROM regions
                LEFT JOIN lands
                ON regions.id_lands = lands.id";
    $rows = array();
        while ($data=mysql_fetch_assoc($sql)){
            $rows['region'] = $data;                                
        }
    echo json_encode( $rows  );
}
?>

2 个答案:

答案 0 :(得分:0)

这里不需要json。你可以简单地使用jquery和ajax

jquery的:

function get_region(country_id) {
    if (country_id != 0) {
        $("#region_id").html("<option value='0'>Select Region</option>");
        $("#region_id").prop("disabled", true);

        $.post("ajax/ajax.php", {
            country_id: country_id
        }, function (data) {

            var data_array = data.split(";");
            var number_of_name = data_array.length - 1;
            var value;
            var text;
            var opt;
            var temp_array;

            for (var i = 0; i < number_of_name; i++) {
                temp_array = data_array[i].split(",");
                value = temp_array[1];
                //alert(value);
                text = temp_array[0];
                opt = new Option(text, value);
                $('#region_id').append(opt);
                $(opt).text(text);
            }
            $("#region_id").prop("disabled", false);
        });
    } else {
        $("#region_id").html("<option value='0'>Select Region</option>");
        $("#region_id").prop("disabled", true);
    }
}

ajax文件是ajax.php

if (isset($_POST["country_id"])) {
    $country_id = $_POST["country_id"];
    $region_select = mysql_query("select * from region where country_id='$country_id'");
    $region = "";
    $region_id = "";
    while ($region_row = mysql_fetch_array($region_select)) {
        $region = $region.$region_row["region"].
        ",".$region_id.$region_row["id"].
        ";";
    }
    echo $region;
}

HTML OF REGION SELECT BOX:

<select name="region_id" id="region_id" disabled="disabled">
    <option value="0">Select Region</option>
</select>

出于安全考虑,您可以将mysql_query更改为PDO,因为mysql_query已被取消。

答案 1 :(得分:0)

检查一下,对我有用。

JS:

jQuery(document).ready(function() {
    var region = jQuery('#region');
    var land = jQuery('#land').change(function() {
        jQuery.post(
            'getList.json.php', {
                'land': land.val()
            },
            function(data) {
                jQuery('#region').empty();
                if (data != null) {
                    region.append(data);
                }
                else {
                    region.append('<option value="">Please select...</option>');
                }
            },
            'html'
        );
    });
 });

PHP:

if($_POST && $_POST['land'] != "") {
    $sql="SELECT region
            FROM regions r
            LEFT JOIN lands l ON r.id_lands = l.id
           WHERE l.city = " . $_POST['land'];
    $result = mysql_query($sql); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< UPD!
    while ($data = mysql_fetch_assoc($result)) {
        echo '<option value="' . $data['region'] . '">' . $data['region'] . '</option>';
    }
}