简体   繁体   中英

Auto-submitting dynamic select form

I'm having trouble with my dynamic select forms. When the "subject" form is submitted it works perfectly, but when the "course" form is then submitted, the form seems to submit properly (localhost/page.php?subject=1&course=2), but it returns the option back to null so it isn't working with later php that is dependent on the course_id .

Header PHP:

<?php
$subject = $course = null;

$conn = mysql_connect('', '', '');
$db = mysql_select_db('',$conn);

if(isset($_GET["subject"]) && is_numeric($_GET["subject"]))
{
     $subject = $_GET["subject"];
}

if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
     $country = $_GET["course"];
}
?>

Javascript:

<script language="JavaScript">

function autoSubmit()
{
     var formObject = document.forms['theForm'];
     formObject.submit();
}

</script>

HTML Form:

<form name="theForm" method="get">
    <select name="subject" onChange="autoSubmit();">
        <option value="null">Select a Subject...</option>

        <?php
        $sql = "SELECT DISTINCT subj_name, subj_id FROM table1 ORDER BY subj_name";
        $result = mysql_query($sql) or die ("couldn't execute query");

        while($row = mysql_fetch_array($result))
        {
            echo ("<option value=\"$row[subj_id]\" " . 
                 ($subject == $row["subj_id"] ? " selected" : "") . ">$row[subj_name]</option>");        
        }
        ?>          
    </select>

<?php
if($subject != null && is_numeric($subject))
{
?>

    <select name="course" onChange="autoSubmit();">        
        <option value="null">Select a Course...</option>

        <?php
        $sql = "SELECT DISTINCT course_id, course_name, subj_id FROM table1 WHERE subj_id = $subject";
        $result = mysql_query($sql);

        while($row = mysql_fetch_array($result))
        {
            echo ("<option value=\"$row[course_id]\" " . 
                     ($course == $row["course_id"] ? " selected" : "") . ">$row[course_name]</option>");        
        }
        ?>          
    </select>

<?php
}
?>
</form>

In these lines "selected" should only be set if $subject and $course are not passed in callback request:

<option value="null" selected="selected">Select a Subject...</option>    
...
<option value="null" selected="selected">Select a Course...</option>

Otherwise, you'll get two options marked as selected in each dropdowns. This cause to bug.

I found the error: $country in line 14 needs to be $course :

if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
 $course = $_GET["course"];
}
?>
<pre>
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
     $country = $_GET["course"];
}
</pre>


Here, you are setting a $country variable instead of $course.

**Use this one**

$course= $_GET["course"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM