Edit Radio Button / Dropdown list
I would like to edit and update a form. However, im not sure how to display the data from the dbase for radio button or drop down list.
For example if its a input text field :
Pages : <input type="text" name="txtpages" id="txtpages" value="<?php echo $rows['pages'];?>" />
How do i do this for a radio button/ drop down list?
Lets say dis is my code :
Media Type :
<select name="mediaList" id="mediaList">
<option selected="Selected">Physical Only</option>
<option>Digitized Only</option>
<option>Physical + Digitized</option>
<option>Digit开发者_StackOverflow中文版al Files</option>
</select>
</div></td>
`
Thank you.
it depends on the value of your radio.
<input type=radio value="yes" <?php echo ($rows['blabla']=='yes') ?'checked="checked"':''; ?> />
just in case you are wondering what that php code is :) it's an if statement that checks if a value is true then print out checked="checked" which indicates the radio button is selected.
edit
<select name="mediaList" id="mediaList">
<option <?php echo ($rows['medialist'] =='Physical Only')?'selected="Selected"':'';?> >Physical Only</option>
</select>
from the above example
first we have connect to database and retrive the data available on that particular column and
using a simply if condition
code started
<? $hostname = 'localhost'; // Your MySQL hostname.
$username = ''; // Your database username.
$password = ''; // Your database password.
$dbname = ''; //your database name
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
$sql=mysql_query("select * from yourtablename")or die (mysql_error());
while($row=mysql_fetch_array($sql)){ ?>
<select name="mediaList" id="mediaList">
<option value="2011" <?php if( $row['year']=='2011' ){echo "selected";}
?>>2011</option>
<option value="2012" <?php if($row['year']=='2012'){echo"selected"; } ?>>2012</option>
</select>
</div></td>
here 'year'is my column name in my database
Assuming the number of available pages can grow large, youll probably want to use a Select for this, a multiple select if they can use multiple options...
<select name="txtpages">
<?php foreach($rows['pages'] as $value => $label): ?>
<?php echo sprintf('<option value="%s">%s</option>', $value, $label); ?>
<?php endforeach; ?>
</select>
For drop down if your database table field name is "area_of_interest" then you fetch it and then check like below:
<tr>
<?
if($data['area_of_interest']=="day")
{
$p="selected";
}
else if($data['area_of_interest']=="night")
{
$r="selected";
}
?>
<td>
Area of Interest:-
</td>
<td>
<select id="shift" name="shift">
<option id="day" value="day" <?=$p?>>Day Shift</option>
<option id="night" value="night" <?=$r?>>Night Shift</option>
</select>
</td>
</tr>
精彩评论