2011-07-17 135 views
1

我遇到了動態選擇表單的問題。當提交「主題」表單時,它完美地工作,但當提交「課程」表單時,表單似乎正確提交(localhost/page.php?subject = 1 & course = 2),但它返回選項回到null,所以它不適用於後來的PHP,這是依賴於course_id自動提交動態選擇表格

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

回答

0

我發現了錯誤:$country在第14層的需求是$course

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

在這些行 「中選擇了」 只應如果$主題,$當然是設置未通過回撥請求:

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

否則,您會在每個下拉列表中標記爲選中的兩個選項。這導致錯誤。

0
<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"];