2011-08-23 34 views
0

好吧,我有一個選擇框(下拉菜單)和一組單選按鈕。將值添加到表單的單選按鈕並使用PHP選擇[輸入類型]

選擇你的類別:

<form action="" method="post"> 
<select name="class"> 
<option value="a">A</option> 
<option value="b">B</option> 
</select> 
<br><br> 

Red or blue:<br> 
<input type="radio" name="red" value="red">Red 
<input type="radio" name="blue" value="blue">Blue 
</form> 

我已經連我的PHP腳本到數據庫,並從表中檢索數據並存儲它們的變量要呼應出。 ROW是Mysql讀取數組。

$class = $row['class'] 
$color = $row['color'] 

我如何能在單選按鈕,選擇框顯示出來(通常爲文本框我的價值的方式設置爲 <? php echo $foo ?>

有人能幫助我嗎?

回答

0
//read the data from database 

$row=mysql_fetch_row($result); 
//assuming that the first data cell fetched is for dropdown list and the second is for radio button thats why $row[0] and $row[1] is used 
<form action="" method="post"> 
<select name="class"> 
<option value="a" <?php if($row[0]=='a'){?> selected <?php } ?> >A</option> 
<option value="b" <?php if($row[0]=='b'){?> selected <?php } ?> >B</option> 
</select> 
<br><br> 

Red or blue:<br> 
<input type="radio" name="red" value="red" <?php if($row[1]=='red'){?> checked<?php } ?>>Red 
<input type="radio" name="blue" value="blue" <?php if($row[1]=='blue'){?> checked<?php } ?>>Blue 
</form> 
+0

是你的選擇=「選擇」和檢查=「檢查」 – edward

+0

感謝您的幫助 – edward

相關問題