weixin_33691598 2018-05-28 05:46 采纳率: 0%
浏览 48

动态下拉菜单不起作用

I am trying to implement a dynamic drop-down list using Ajax and PHP. Based on the index value in the first option list, second one should give me list of names with that id.

select1.php :

<html>
<head>
<link rel="stylesheet" type="text/css" href="select_style.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function fetch_select(val)
{
 $.ajax({
 type: 'post',
 url: 'fetch1.php',
 data: {
  get_option:val
 },
 success: function (response) {
  document.getElementById("new_select").innerHTML=response; 
 }
 });
}

</script>

</head>
<body>
<p id="heading">Dynamic Select Option Menu Using Ajax and PHP</p>
<center>
<div id="select_box">
 <select onchange="fetch_select(this.value);">
  <option>Select ID</option>
  <?php
  $host = 'localhost';
  $user = 'admin';
  $pass = 'admin';
  $dbname='kancha';
  $conn = new mysqli($host, $user, $pass, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  } 

  $sql = "select distinct id from test";
  $select= $conn->query($sql);
  if ($select->num_rows > 0) {
    while($row = $select->fetch_assoc()) {
        echo "<option value='".$row['id']."'>".$row['id']."</option>";
       //echo "<option value=>".$row['id']."</option>";
    }
  } else {
    echo "0 results";
  }
  ?>
 </select>

 <select id="new_select">
 </select>

</div>     
</center>
</body>
</html>

fetch1.php

<?php
if(isset($_POST['get_option']))
{
  $host = 'localhost';
  $user = 'admin';
  $pass = 'admin';
  $dbname='kancha';
  $conn = new mysqli($host, $user, $pass, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  } 

  $id = $_POST['get_option'];
  //echo '$id';
 $sql = "select id, name from test where id='$id'";;
 $find= $conn->query($sql);

 if ($find->num_rows > 0) {
    while($row = $find->fetch_assoc()) {
        //echo "<option>".$row['name']."</option>";
        echo "<option value='".$row['id']."'>".$row['name']."</option>";
    }
  } else {
    echo "0 results";
  }
 exit;
}
?>

My database looks something like this :

SELECT * from test; 
id  name
1   Name1
2   Name2
1   Name3

The first drop down works just fine. However the second drop down isn't working. I have attached the screenshot of it as well. Is there a problem in sending data across the files or what? Not able to figure out where has the code gone wrong.

For the second option list, when I have selected 1, I should be getting Name1 and Name3 as options but I get none. enter image description here

EDIT: Corrected javascript in select1.php

  • 写回答

2条回答 默认 最新

  • 程序go 2018-05-28 05:54
    关注

    You are setting the content for new_select with the wrong variable.

    It should be response rather than val

    Change to:

    document.getElementById("new_select").innerHTML=response; 
    

    And assign value to your return options.

    Like:

    echo "<option value='".$row['id']."'>".$row['name']."</option>";
    

    And change your sql string to the following for the above to work.

     $sql = "select id, name from test where id='$id'";
    

    And make sure your jquery.js include is being loaded.

    评论

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退