doulan1073 2015-05-01 08:02
浏览 45
已采纳

依赖的下拉列表不起作用Mysql Php - 返回`Undefined`

I have two dropdowns generating from MySQL database. The second dropdown is based on the first dropdown select option.

The problem is that, I am unable to generate the second dropdown data.

I am getting the second dropdown values as Undefined. But the number of values that should come in the second dropdown is correct.

Please suggest where am I doing wrong. Thanks.

Below are my codes:

Index.php (connection)

<?php  
//Create the connection  

$con = mysqli_connect("localhost","root","root","echodeve_mfb_temp") or die("Some error occurred during connection " . mysqli_error($con));  

// Write query

$strSQL = "SELECT bp_id, bp_name FROM mfb_billing";

// Execute the query.

$query = mysqli_query($con, $strSQL);


// Close the connection
//mysqli_close($con);

?>

Index.php (Script)

  <script>
$(document).ready(function() {

    $("#item_1").change(function () {   

      var group_id = $(this).val();

       $.ajax({
            type: "POST", 
            url: "dropdown_select.php?item_1_id=" + group_id, 
            dataType: "json",
            success: function(data){
                //Clear options corresponding to earlier option of first dropdown
                $('select#item_2').empty(); 
                $('select#item_2').append('<option value="0">Select Option</option>');
                //Populate options of the second dropdown
                $.each( data, function(i, val){    
                    $('select#item_2').append('<option value="' + val.hospital_id + '">' + val.hospital_name + '</option>');
                });
                $('select#item_2').focus();
            },
            beforeSend: function(){
                $('select#item_2').empty();
                $('select#item_2').append('<option value="0">Loading...</option>');
            },
            error: function(){
                $('select#item_2').attr('disabled', true);
                $('select#item_2').empty();
                $('select#item_2').append('<option value="0">No Options</option>');
            }
        })  

    }); 
});

</script>

  </head>

Index.php (HTML)

  <body>
 <label id="item_1_label" for="item_1" class="label">#1:</label>
<select id="item_1" name="item_1" />
    <option value="">Select</option>
    <?php

        while($row = mysqli_fetch_array($query)) { 
            echo '<option value="'.$row['bp_id'].'">'.$row['bp_name'].'</option>'."
";       
        }
     ?>
</select>

<label id="item_2_label" for="item_2" class="label">#2:</label>
<select id="item_2" name="item_2" />                        
</select>



   </body>

dropdown_select.php (Processing PHP)

    <?php

$item_1_id = $_GET['item_1_id'];

//Create the connection  

$con = mysqli_connect("localhost","root","root","echodeve_mfb_temp") or die("Some error occurred during connection " . mysqli_error($con));  

// Write query

$strSQL = "SELECT hospital_id, hospital_name FROM mfb_hospital WHERE bp_id = $item_1_id";

// Execute the query.

$query = mysqli_query($con, $strSQL);

$return_arr = array(); 

while($row = mysqli_fetch_array($query)) { 

    $row_array = array("name" => $row['hospital_name'], 
                       "id" => $row['hospital_id']); 

    array_push($return_arr,$row_array);     
}

echo json_encode($return_arr);

?>  
  • 写回答

1条回答 默认 最新

  • dongqian6554 2015-05-01 08:20
    关注

    PHP error exactly tells you what is going wrong and as manual says: Returns FALSE on failure .

    Since your error describres $query as boolean given you have to check several thing on your own.

    First echo your $strSQL and see if it's proper query, then try to execute it directly on database. There maybe several things, like inproper name of column.

    Also, you are returning:

    $row_array = array("name" => $row['hospital_name'], 
                           "id" => $row['hospital_id']); 
    

    Yet in javascript you are doing:

    $.each( data, function(i, val){    
        $('select#item_2').append('<option value="' + val.hospital_id + '">' + val.hospital_name + '</option>');
    });
    

    try this:

    $.each( data, function(i, val){    
        $('select#item_2').append('<option value="' + val.id + '">' + val.name + '</option>');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取