2011-06-08 70 views
0

我應該怎樣在「select」元素上正確刷新PHP中的頁面?在PHP中提交onChange

在我的作業中,我有2個組合框1)車輛類型和2)特定於該類型的顏色 每次我選擇一種類型,我需要刷新鏈接的顏色。

看起來onchange="this.form.submit()"不起作用!

這是我的代碼:

<?php 
include_once ("config.php"); 

$link = mysql_connect($host,$user,$pass) or die("Could not connect : " . mysql_error()); 
mysql_select_db($db_name) or die("Could not select database"); 
?> 
<html> 
<head> 
</head> 
<body> 
<?php 
$menu_type=$_POST['menu_type']; 
echo 'menu_type='.$menu_type.'<br/>'; 
echo 'empty='.(bool)empty($menu_type).'<br/>'; 
echo '-1='.(bool)($menu_type==-1); 
?> 
<table> 
    <form name="myform" action="ajoute.php" method="POST" > 
    <tr> 
     <td>Matricule</td> 
     <td><input type="text" value="" style="width:150px;" name="Matricule"></td> 
    </tr> 
    <tr> 
     <td>Vehicule</td> 
     <td>Couleur</td> 
    </tr> 
    <tr> 
     <td><select name="menu_type" style="width:150px;" onchange="this.form.submit()"> 
     <option value="-1">Select voiture type</option> 
      <?php 
      $resType = mysql_query("SELECT * FROM type_vehicule") or die(mysql_error()); 
      for ($i=0; $i<mysql_num_rows($resType); $i++) 
      { 
       $row=mysql_fetch_array($resType); 
       echo "<option value = ".$row['ID'].">".$row['nom']."</option>"; 

      }?> 
     </select>  
     </td> 
     <td> 
     <?php 
     echo "empty = ".(bool)empty($menu_type)."; $menu_type = ".$menu_type; 
     ?> 
     <select name="menu_couleur" size="1" style="width:150px;"> 

     <?php  

     if (empty($menu_type) || $menu_type == -1) 
     { 
      echo "<option value='-1'>Select voiture type</option>"; 
     } 
     else 
     { 
      $type=(int)$menu_type; 
      $result=mysql_query("SELECT c.Id, c.Couleur FROM couleur c inner join 
      type_couleur tc on c.id=tc.Couleur_ID where tc.type_id=".$type); 
      while ($row = mysql_fetch_array($result)) { 
      echo "<option value=".$row['ID'].">".$row['couleur']."</option>"; 
      } 
    } 
?> 
     </select> 
     </td> 
    </tr> 
    <tr> 
     <td align="right" colspan=2><input type="submit" name="submit" value="Valide"></td> 
    </tr> 
    </form> 
</table> 

<?php 

mysql_free_result($resType); 
mysql_close($link); 
?> 
</body> 
</html> 
+0

是否有javascript錯誤? (使用螢火蟲進行檢查) – hakre 2011-06-08 23:50:27

+0

onchange看起來是正確的,在這裏工作,否則請嘗試getElementById('formidtarget')。submit();與一個ID添加到

,我也注意到你沒有機制重新選擇頁面重新加載時的「menu_type」中的值? – Scuzzy 2011-06-09 00:05:06

回答

1

的問題是,因爲你命名你的提交按鈕提交(即名稱=「提交」)。所以當你做一個this.form.submit()時,它試圖在html元素上執行一個函數。

在Chrome中的javascrip控制檯我得到的錯誤:

document.formname.submit不是一個函數遺漏的類型錯誤:房產「提交」對象#HTMLForm元素是不是一個函數

做了谷歌搜索並想出了這個網頁,解釋了一切:http://eee94180.blogspot.com/2008/09/javascript-formsubmit-is-not-function.html

所以你解決您的問題只是刪除名稱=「提交」您的提交按鈕或命名爲別的東西,如姓名=「提交按鈕」。

+0

我可以在點擊驗證(提交)按鈕和OnChange組合框之間進行區分嗎? – serhio 2011-06-09 01:02:31

0

首先,您已將表格寫入表格標籤內,這是不正確的。 它應該是這樣的

<form> 
<table> 
... 
</table> 
</form> 

獲取HTML輸出,看看你在控制檯,便於識別得到任何JavaScript錯誤。同時檢查提交按鈕名稱。

+0

無所謂。 – dar7yl 2011-06-09 01:54:17