jQuery填充数据库中的级联下拉列表

时间:2012-06-07 12:50:19

标签: php jquery mysql drop-down-menu

Site here.

我正在尝试使一个表单从数据库中触发另一个表单。我已经对HTML,PHP和数据库进行了排序,但我正在努力使用jQuery。

有人能让我朝着正确的方向前进吗?我已经用谷歌搜索过了,但由于我不确切知道要搜索什么,我受到了一些限制。

编辑:

以下是我的建议。不幸的是它仍然没有用。

jQuery的:

<script type="text/javascript">                       
$('#city').change(function(){
    var $club = $('#club');
    $club.find('option:not([value="default"])').remove(); //Remove previous items
    $.getJSON('GetClubs.php', {city:$(this).val()}, function(clubs){
        $.each(clubs, function(index, city){
            $club.append('<option value="'+city[0]+'">'+city[1]+'</option>');
        });
    });
});
</script>

HTML:

<form name="myform" action="" method="POST">
<h1>1. Choose your city</h1>
<select name="city" class="dropdown" id="city">
<option value="default" disabled="disabled" selected="selected">--- Select your option ---</option>
<?php getCities(); ?>
</select>

<h1>2. Choose your club</h1>
<select name="club" class="dropdown" id="club">
<option value="default" disabled="disabled" selected="selected">--- Select your option ---</option>
</select>
</form>

PHP:

<?php

date_default_timezone_set('Europe/London');

$day = date("l");
$time = date("G");

if ($time >= 21) {
    $day = date('l', strtotime($day .' +1 day'));
    }

$city = $_POST['city'];


if ($day == Monday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')";
    }

else if ($day == Tuesday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY')";
    }

else if ($day == Wednesday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY')";
    }

else if ($day == Thursday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY')";
    }

else if ($day == Friday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY')";
    }

else if ($day == Saturday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'SATURDAY', 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY')";
    }

else if ($day == Sunday) {
    $query = "SELECT name FROM nights WHERE city = '$city' ORDER BY FIELD(day, 'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY')";
    }

    $result = mysql_query($query);
    $items = array();

    if($result && mysql_num_rows($result) > 0) { 
        while ($row = mysql_fetch_array($result)) { 
            $items[] = array( $row[0], $row[1] );
            }         
    } 

    mysql_close(); 
    // convert into JSON format and print
    echo json_encode($items);
?>

2 个答案:

答案 0 :(得分:0)

$('#city').change(function(){
    var $club = $('#club');
    $club.find('option:not([value="default"])').remove(); //Remove previous items
    $.getJSON('GetClubs.php', {city:$(this).val()}, function(clubs){
        $.each(clubs, function(index, city){
            $club.append('<option value="'+city[0]+'">'+city[1]+'</option>');
        });
    });
});

您还必须删除echo "<option..."

您还应该查看mysql-real-escape-string

答案 1 :(得分:0)

Check this

让你的第一个下拉列表在“onChange”事件中调用jquery函数。在此函数中1)获取所选选项的值,2)使用上面链接中的示例创建一个ajax,如下面的代码,并将值传递给ur GetClubs.php。

    $.getJSON('url/GetClubs.php', function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<option value="' + key + '">' + val + '</option>');
  });

  $('<select/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('#secondDropdown');
});

当然,这个例子依赖于JSON文件的结构:

{
  "one": "Singular sensation",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}

多数民众赞成。在第一个选择框

中更改值时会填充数据