如何在表单提交上捕获第二个列表

时间:2017-04-12 04:32:44

标签: php sql forms

我有一个两级动态list.its工作正常现在问题是我无法捕获第二个列表值...我想你们可以帮助我..

这是表格

    <?php
    require 'db.php';
    ?>


    <!DOCTYPE html>
    <html>
    <head>
      <title>Selection Form</title>
     </head>

    <body>
    <form action="/display/check.php" method="get"/>

    <table>
        <tr>
        <td>Select Semester</td>
        <td>
            <select id=semesterdd onChange="change_semester()" name="sem">

                <option>
                    Select
                </option>
                    <?php
                        $result = $mysqli->query("SELECT * FROM subjects ORDER BY semester ASC") or die($mysqli->error());
                        while($row=mysqli_fetch_array($result))

                        {
                            ?>
                            <option ><?php echo $row["semester"];?></option>

                            <?php
                        }   
                    ?>

            </select>
        </td>
    </tr>

    <tr>
        <td>
            Select Subjects:  
        </td>
        <td> 
            <div id="subjectid">
                <select name='sub'>
                    <option>
                        Select
                    </option>
                </select>
            </div>
        </td>

    </tr>
    </table>

    <input type="submit" value="submit">
    </form>

    <script type="text/javascript">
        function change_semester()
        {

            var xmlhttp=new XMLHttpRequest();
            xmlhttp.open("GET","ajax3.php?semester="+document.getElementById("semesterdd").value,false);
            xmlhttp.send(null);
            alert(xmlhttp.responseText);
            document.getElementById("subjectid").innerHTML=xmlhttp.responseText;

        }

    </script>

</body>

这是ajax文件

<?php

require 'db.php';

$semester=$_GET["semester"];

if($semester!="")
{   
    $result=$mysqli->query("SELECT * FROM subjects WHERE semester=$semester ORDER BY subject ASC") or die($mysqli->error());
    echo "<select>";
    while($row=mysqli_fetch_array($result))

    {

                echo "<option value=>";echo $row["subject"];echo "</option>";
                //echo '<option value="'.$row['code'].'">'.$row['subject'].'</option>';



    }   
    echo "</select>";
}   

?>

这是响应..它捕获了学期选项,但为什么不选择子选项?

http://127.0.0.1/display/check.php?sem=3

1 个答案:

答案 0 :(得分:0)

xmlhttp.open("GET","ajax3.php?semester="+document.getElement‌​ById("semesterdd").v‌​alue+"&sub="+document.getElement‌​ById("[subjectid]").v‌​alue,false);

将[subjectid]更改为您想要的任何ID。