2012-08-01 39 views
0

我的代碼在某處存在問題,當用戶在第一個選擇菜單中選擇「事件」時,它會使用數據庫中的選項更新第二個菜單。第二個菜單在某些瀏覽器(例如IE和Chrome)中沒有更新,但是在Firefox等其他瀏覽器中工作正常。javascript document.submit在所有瀏覽器中都不起作用

我不太清楚什麼是問題,任何幫助將不勝感激。 謝謝!

我目前擁有的代碼如下─

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<script type="text/javascript"> 
function events() 
{ 
    document.eventform.submit(); 
} 
</script> 

<? 
require_once('dbconnect.php'); 

$sql="SELECT id, event FROM events ORDER BY id DESC"; 
$result=mysql_query($sql); 

$options1=""; 

while ($row=mysql_fetch_array($result)) { 

$id=$row["id"]; 
$event=$row["event"]; 
$options1.="<OPTION VALUE=\"$event\">".$event;'</OPTION>'; 

} 
$eventis = $_GET['event']; 
$sql = "SELECT * FROM events WHERE event='$_GET[event]' ORDER BY id DESC"; 
$result=mysql_query($sql); 

$options2=""; 

while ($row=mysql_fetch_array($result)) { 

$id=$row["id"]; 
$location1=$row["location1"]; 
$location2=$row["location2"]; 
$location3=$row["location3"]; 
$location4=$row["location4"]; 
    $options2.="<OPTION VALUE=\"$location1\">".$location1;'</OPTION>'; 
$options2.="<OPTION VALUE=\"$location2\">".$location2;'</OPTION>'; 
$options2.="<OPTION VALUE=\"$location3\">".$location3;'</OPTION>'; 
$options2.="<OPTION VALUE=\"$location4\">".$location4;'</OPTION>'; 

} 
?> 
</head> 
<body> 


<form action="" name="eventform" class="style1" id="eventform" onchange="events()"> 
    <p align="center"> 


Step 1: 
    <select name="event" id="event"> 
    <OPTION VALUE=0>Select Event: 
    <?=$options1?> 
    </select> 
    &nbsp;</label> 
    <?php echo $eventis ?></p> 
    <div align="center"></div> 
    <div align="center"></div> 
</form> 

<form action='sale.php' name= 'locationform' method='get'> 

<div id="eventname"></div> 
<p align="center">Step 2: Select the location: 
    <select name="location" id="location"> 
<OPTION VALUE=0>Select Location: 
    <?=$options2?> 
</select> 
</p> 
<p align="center"> 
<input name="event" type="hidden" id="event" value="<?php echo $eventis ?>"> 
<input name="operator" type="hidden" id="operator" value="<?php echo $operator ?>"> 
<input type="submit" value="Submit" /> 
</form> 
</p> 

</body> 
</html> 
+0

它是有效的HTML?如果沒有,那麼你不能指望它工作(雖然無效的HTML是非常高的容忍)。另外,你可能想看看SQL注入。 – Corbin 2012-08-01 03:24:50

+0

onchange應該不是在選擇而不是表格上? ..但理想情況下,您不會提交表單的選擇值的更改,而只是將選項更改爲第二個選擇(無論是通過ajax還是使用第二選擇的幾個預先加載的選項集之一)。 – ghbarratt 2012-08-01 03:27:43

+0

謝謝ghbarratt,這正是問題所在! – chris222 2012-08-02 07:25:54

回答

0

那麼對於初學者你不選擇你的形式

document.eventform.submit();

不選擇你的表格,你將需要使用

document.forms["eventform"].submit()

或使用getElementById選擇表單。

其次你的html是有點災難:)但我不認爲這是問題。只是想我會讓你知道的。

0

添加結束標記在這兩種情況下 <OPTION VALUE=0>Select Event:</OPTION> <OPTION VALUE=0>Select Location:</OPTION>

+0

此外,將值屬性包裝在引號中是一種很好的做法;) – inhan 2012-08-01 03:51:01

相關問題