用ajax选择框更改

时间:2013-08-21 16:54:26

标签: php ajax

我尝试构建选择框,用ajax更改第二个选择框的值。

首先,我选择AREA而不是该地区的CITIES。

你能告诉我我做错了吗?

客户方:

<script>
$(function () {

  $("#first").change(function () {
    $("#second").load('recs.php?area_id=' + $(this).val());
  });

});
</script>


<form method="post" action="tosomewhere.php">

    <select id="first" name="area_id">
      <option value="1">1</option>
      <option value="2">2</option>
    </select>

    <select id="second" name="section">  </select>

</form>

服务器端:

<?PHP

    include "db.php"

    $areaID = $_GET['area_id'];
    $second_option = "";

    $query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC");
    while($index = mysql_fetch_array($query2)) 
    {
        $id = $index['id'];
        $name  = $index['name'];

        $second_option .= "<option value='$id'>$name</option>";
    }

    echo $second_option;

?>

1 个答案:

答案 0 :(得分:0)

试试这个,

$(function () {

  $("#first").change(function () {
    $.get('recs.php', {area_id: $(this).val()}, function(data) {
      $("#second").html(data);
    });
  });

});

注意:最好在exit后添加echo

echo $second_option;
exit;