2012-05-11 44 views
-1

我有一個名爲reg.php的表單,它的動作是reg.php,我希望點擊提交時選定的下拉值要保持選中,我已經完成的工作如下下拉註冊表單不會在提交時刷新,如果有錯誤(在驗證的情況下)

<?php 
if($_POST['registerbtn']){ 
$selected_value = $_POST['selectID']; 
$query = mysql_query("SELECT linecard_name FROM selection WHERE select_id = '$selected_value'"); 
$rows=mysql_fetch_assoc($query); 
$linecard_name= $rows['linecard_name']; 
$sql = "SELECT select_id, linecard_name FROM selection " . "ORDER BY linecard_name"; 
$rs = mysql_query($sql); 

while($rownw = mysql_fetch_array($rs)){ 
if( $rownw['linecard_name'] == $linecard_name) { 
$options = "<option selected =selected value=".$rownw['select_id']."> " .$rownw['linecard_name']. " </option> "; 
} 

} 
} 


require("./connect.php"); 
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name"; 
$rs = mysql_query($sql); 

while($rownw = mysql_fetch_array($rs)){ 
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> "; 
} 
mysql_close() ; 

$form = "<form action='./reg.php' method='post'> 
<table> 
<tr> 
<td> </td> 
<td> <font color='red'> $errormsg </font> </td> 
</tr> 


<tr> 
<td> Select Linecard </td> 
<td> <Select name='selectID' > <option value = '0'> Select from here </option> $options </select></td> 

<tr> 
<td > <input type='submit' name='registerbtn' value='Register' /> </td> 
</tr> 
</table> 
+0

檢查我的回答..之後,就是你想要的嗎? – sujal

+0

我會看到給我幾分鐘 – Sparkle

回答

0

如果您確保表單的動作是在同一個頁面的形式給出,你將能夠訪問發佈的數據。

0

提交表單才能發佈,值和下拉值進行比較,即

<?php 
require("./connect.php"); 
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name"; 
$rs = mysql_query($sql); 
$options = "<option value= '0' > Select from here </option>"; 
$_POST['prefix']=isset($_POST['prefix']) ? $_POST['prefix'] : ''; 
    while($rownw = mysql_fetch_array($rs)){ 
    $sel=''; 
     if($rownw['select_id']==$_POST['prefix']){ 
      $sel="selected='selected'"; 
     } 
    $options .= "<option value = '".$rownw['select_id']."' ".$sel." > ".$rownw['linecard_name']. " </option> "; 
    } 

mysql_close() ; 

$form = "<form action='./reg.php' method='post'> 
<table> 
<tr> 
<td> </td> 
<td> <font color='red'> $errormsg </font> </td> 
</tr> 

<tr> 
<td> Username </td> 
<td> <input type='text' name='user' /> </td> 
</tr> 

<tr> 
<td> Select Linecard </td> 
<td> <Select name='selectID'> $options </select> 

試試這個

+0

嘿@sujal ...我想獲得保留下面的代碼的下拉值也可以幫助嗎? ​​ Sparkle

+0

檢查我的答案..根據您的代碼 – sujal

+0

hey @sujal:它不保留提交時的選定值?? – Sparkle

相關問題