0

网站在这里。

我正在尝试使一种形式从数据库中触发另一种形式的填充。我已经对 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);
?>
4

2 回答 2

0

检查这个

让您的第一个下拉列表在“onChange”事件中调用 jquery 函数。在此函数中 1) 获取所选选项的值,2) 使用上面链接中的示例创建一个 ajax,如下面的代码,并将值传递给您的 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"
}

而已。在第一个选择框中的值更改时填充数据

于 2012-06-07T13:07:33.053 回答
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>');
        });
    });
});

You'll also have to remove the echo "<option...".

You should also have a look at mysql-real-escape-string

于 2012-06-07T13:01:45.437 回答