如何使用数据库值填充动态附加div中的下拉列表

时间:2015-11-01 05:17:53

标签: javascript php mysql ajax

我想在我附加的div中使用php,mysql和ajax创建一个下拉列表,但它不适用于我.javascript代码位于城市和地区来自ajax和php并在动态div中创建+按钮点击: 使用Javascript:

<script>
$(document).ready(function () {
var counter = 0;
$("#addButton").click(function () {
        if (counter > 19) {
            alert("Only 20 Address allowed");
            return false;
        }


    var elems = '<div class="col-lg-5"  id="Address' + counter + '">'+
          '<textarea class="form-control" name="alt_address[]" rows="3" placeholder="Address' + (counter+1) + '" /><div class="col-lg-6 form-group col-lg-offset-6"> </div>' +
          '<div class="col-lg-3 form-group" id="city' + counter + '">'+
          '<select name="city_name[]" id="city_name' + counter + '" class="form-control"></select>' +
          '</div><div class="col-lg-3 form-group"><select name="locality_name[]" id="locality_name' + counter + '" class="form-control"><option value="" selected="selected" >Select Locality</option></select></div></div>' + 
          '<div class="col-lg-1 form-group">'+
          '<button type="button" class="removebtn" id="removeButton' + counter + '">' +
          '<span class="glyphicon glyphicon-minus"></span></button>' +
          '</div><div class="col-lg-6 form-group col-lg-offset-6"> </div>' ;


    $('#addressDiv').append(elems);

        counter++;

         $.ajax({

        type:"Post",

        url:"city_load.php",



        cache:"false",

        success:function(html){

            $("#locality_name" + counter).html(html);

        }

        });

         return false;
         });

         $("#city_name" + counter).change(function(){



    $city_id = $(this).val();



    $.ajax({

        type:"Post",

        url:"ajax-dd3ck.php",

        data:"city_id="+$city_id,

        cache:"false",

        success:function(html){

            $("#locality_name" + counter).html(html);

        }

        });

    });



                $(document).on('click','.removebtn',function () {

                if(counter==0){
      alert("No more textbox to remove");
      return false;
   }   

counter--;

    $("#Address" + counter).remove();
    $("#removeButton" + counter).remove();






});


});

1 个答案:

答案 0 :(得分:0)

这是语法错误:

while($row=mysqli_fetch_array($res))
     echo "<select name='city_name' id='city_name'$i class='form-control'>";
{

那应该是

echo "<select name='city_name' id='city_name'$i class='form-control'>";
while($row=mysqli_fetch_array($res)) {

id='city_name'$i也是HTML错误,但不应影响PHP。

你实际上并没有在其他任何地方使用$i或增加它,所以我不确定你为什么会这样做..