2015-05-02 40 views
0

im將數據庫中的主題列表打印到下拉列表中。當選擇一個主題時,下一個名爲section的下拉菜單應該通過在數據庫中查找主題名稱來顯示主題的各個部分。在下面的代碼中使用下拉式鏈接

這是我能夠做的,但它沒有工作,任何幫助的人,請忽略我的編碼風格,因爲我是初學者。先謝謝你。

主要代碼:

<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> 
+0

使它正確nChange ='getSection它將onchange ='getSection – Saty

+0

你可以更清楚地知道你的代碼在什麼時候停止工作。是的,也許javascript函數沒有被調用,或者可能是XHR,甚至是數據庫錯誤。你必須想出調試方法。 (提醒控制檯日誌響應..螢火蟲工具等,..) – animaacija

+0

@saty固定,但仍然代碼將無法正常工作。 – Sam

回答

0

的問題似乎是在該行

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

this.value沒有按照您的意願傳遞。 this這裏的值是一個window對象而不是select標記對象。

所以我建議你在getSection函數本身中獲取當前值的值,並在函數內部形成url然後執行http請求。

+0

啊謝謝你的幫助,但我不是那麼好笑。嗯任何小指導提示? – Sam

+0

我認爲調試代碼的最好方法是先定位可能發生錯誤的區域。在JavaScript中,您可以使用console.log函數在程序的每一步打印Web控制檯上的日誌。 –