如何使用php和ajax动态创建两个下拉框

时间:2015-12-31 14:21:51

标签: php jquery ajax get dropdown

请帮助我解决这个问题。基本上,我想创建两个相互通信的下拉菜单。第一个下拉菜单将包括美国各州。用户选择状态后,我希望其他菜单中的城市显示出来。我将如何进行此操作(使用html,css,php和/或javascript)。我一直遇到$ _get(svalue)的问题。它给出了错误提示:未定义的索引:第82行的C:\ xampp \ htdocs \ online \ ajax-getvalues.php中的svalue。非常感谢任何帮助。

我的桌子看起来像这样 表delcity

city_id  |  cityname  |  svalue
_____________________________________
1        |   Lagos    |  texas
____________________________________
2        |   Abuja    |  Fct
_____________________________________
3        |   Niger    |  mali

和我的代码

<?php
$connection = mysqli_connect("localhost", "apple", "applebed", "gilmorewines");

//$selectvalue = mysql_real_escape_string($connection, $_GET['svalue']);
    $selectvalue= mysql_real_escape_string( $_GET['svalue'],$connection );



mysqli_select_db($connection, "gilmorewines");
$result = mysqli_query($connection, "SELECT cityname  FROM delcity WHERE 'svalue'='$selectvalue' ")or die(mysql_error());

echo '<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result))
  {
    echo '<option value="'.$row['cityname'].'">' . $row['cityname'] . "</option>";
    //echo $row['drink_type'] ."<br/>";
  }

mysqli_free_result($result);
mysqli_close($connection);



?>

Ajax脚本

$(document).ready(function($) {
  var list_target_id = 'list-target'; //first select list ID
  var list_select_id = 'list-select'; //second select list ID
  var initial_target_html = '<option value="">Please select a colour...</option>'; //Initial prompt for target select

  $('#'+list_target_id).html(initial_target_html); //Give the target select the prompt option

  $('#'+list_select_id).change(function(e) {
    //Grab the chosen value on first select list change
    var selectvalue = $(this).val();

    //Display 'loading' status in the target select list
    $('#'+list_target_id).html('<option value="">Loading...</option>');

    if (selectvalue == "") {
        //Display initial prompt in target select if blank value selected
       $('#'+list_target_id).html(initial_target_html);
    } else {
      //Make AJAX request, using the selected value as the GET
      $.ajax({url: 'ajax-getvalues.php?svalue='+selectvalue,
             success: function(output) {
                //alert(output);
                $('#'+list_target_id).html(output);
            },
          error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status + " "+ thrownError);
          }});
        }
    });
});
</script>

0 个答案:

没有答案