找到你要的答案

Q:chained selection using drop down

Q:使用下拉链接选择

in this code below im printing a list of subjects from the database into a dropdown. When a subject is selected, the next drop down called section should display the sections each of subject by looking up for the subject name in the database.

This is what i was able to do but it did not work, any help people and please ignore my style of coding since i am a beginner. Thank you in advance.

main code:

    <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 code:

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

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

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

主要代码:

    <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>
answer1: 回答1:

The problem seems to be in the line

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

this.value is not being passed as you want it to. The value of this here is an window object not the select tag object.

So I suggest you to get the value of current value in the getSection function itself and form the url inside the function and then do the http request.

这个问题似乎是在线

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

this.value是不是你想要的了。这里的值是窗口对象,而不是选择标签对象。

所以我建议你得到的电流值在函数本身和形式的GetSection函数内部的URL然后做HTTP请求。

javascript  php  html  mysqli