链式选择下拉

时间:2015-05-02 22:49:51

标签: php html

我试图这样做:当某人从下拉名称中选择任何一个选项时,下拉部分应显示该主题的所有部分。主题下拉效果很好,取得所有主题的名称,但一个部分不会起作用。我无法找到问题。它应该从数据库中获取部分WHERE / WHEN name(数据库列)等于所选主题。提前致谢。我的代码如下:

我的js代码

    <script type="text/javascript">
function getSection(strURL)
{      
alert(strURL);   
 var req = getXMLHTTP(); // fuction to get xmlhttp object
 if (req)
 {
  req.onreadystatechange = function()
 {
  if (req.readyState == 4) { //data is retrieved from server
   if (req.status == 200) { // which reprents ok status                    
     document.getElementById('sectiondiv').innerHTML=req.responseText;
  }
  else
  { 
     alert("There was a problem while using XMLHTTP:\n");
  }
  }            
  }        
req.open("GET", strURL, true); //open url using get method
req.send(null);
 }
}
</script>

php代码:

<div>
            Subject:

    <?php

    $conn = new mysqli('localhost', '', '', '') 
    or die ('Cannot connect to db');

        $result = $conn->query("select name from class");


        echo "<select name='subject' onchange='getSection('findsection.php?subject=>'this.value'";

        while ($row = $result->fetch_assoc()) {

                      unset($id, $name);
                      $name = $row['name']; 
                      echo '<option value="subject">'.$name.'</option>';

    }

        echo "</select>";
    ?> 
            </div>
    <br>  
    <div id="sectiondiv">
    Section: 
        <select name="select">
     </select>
            </div>

my findsection.php

<? $subject=intval($_GET[‘subject’]);;
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$mysqli  = new Mysqli($servername, $username, $password, $dbname) or mysqli_error($mysqli);
$section = $mysqli->query("SELECT section FROM class WHERE name = '$subject'")->fetch_object()->section; 
$result=mysql_query($section);?>
<select name="section">
<? while ($row = $result->fetch_assoc()) { ?>
   <option value><?=$row['section']?></option>
<? } ?>
</select>

1 个答案:

答案 0 :(得分:0)

你的html不完整/无效

echo "<select name='subject' onchange='getSection('findsection.php?subject=>'this.value'";

将其更改为此

echo '<select name="subject" onchange="getSection(\'findsection.php?subject=\' + this.value)">';