使用下拉链接选择链接

时间:2015-05-02 09:25:50

标签: javascript php html mysqli

在下面的代码中,我将数据库中的主题列表打印到下拉列表中。选择主题后,下一个下拉调用部分应通过在数据库中查找主题名称来显示每个主题的部分。

这是我能够做到的,但它不起作用,任何人帮助,请忽略我的编码风格,因为我是一个初学者。提前谢谢。

主要代码:

    <div>
      Subject:
  <?php
        $conn = new mysqli('fgdfgfg', 'fggfdgfr', 'fgfgf', 'fgfgfgf') or die('Cannot connect to db');
        $result = $conn->query("select name from class");
        echo "<select name='subject' nChange='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>

findsection.php

<? $subject=intval($_GET[‘subject’]);;
$servername = "localhost";
$username = "fhdfhdfhf";
$password = "ghhfghgh";
$dbname = "ghghgh";
$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>

JS代码:

<script type="text/javascript">
function getSection(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>

1 个答案:

答案 0 :(得分:0)

问题似乎在行

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

this.value未按您的要求传递。这里this的值是window对象,而不是select标记对象。

所以我建议你在getSection函数本身中获取当前值的值,然后在函数内部形成url,然后执行http请求。