编辑单选按钮/下拉列表

时间:2010-10-14 03:49:28

标签: php

我想编辑和更新表单。但是,我不知道如何显示dbase中用于单选按钮或下拉列表的数据。

例如,如果是输入文本字段:

Pages : <input type="text" name="txtpages" id="txtpages" value="<?php echo $rows['pages'];?>" />

如何为单选按钮/下拉列表执行此操作?

让我们说dis是我的代码:

  

媒体类型:

          <select name="mediaList" id="mediaList">
            <option selected="Selected">Physical Only</option>
            <option>Digitized Only</option>
            <option>Physical + Digitized</option>
            <option>Digital Files</option>
            </select>

        </div></td>

`

谢谢。

4 个答案:

答案 0 :(得分:1)

取决于收音机的价值。

<input type=radio value="yes" <?php echo ($rows['blabla']=='yes') ?'checked="checked"':''; ?> />

以防万一你想知道php代码是什么:)它是一个if语句,检查一个值是否为true然后print out checked =“checked”表示单选按钮被选中。

修改

<select name="mediaList" id="mediaList">
  <option <?php echo ($rows['medialist'] =='Physical Only')?'selected="Selected"':'';?> >Physical Only</option>
</select>

答案 1 :(得分:1)

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

答案 2 :(得分:0)

假设可用页面的数量可能会变大,您可能希望使用Select for this,如果他们可以使用多个选项,则选择多个选项......

<select name="txtpages">
 <?php foreach($rows['pages'] as $value => $label): ?>
    <?php echo sprintf('<option value="%s">%s</option>', $value, $label); ?>
 <?php endforeach; ?>
</select>

答案 3 :(得分:-1)

如果您的数据库表字段名称为“area_of_interest”,则下拉,然后您获取它,然后检查如下:

   <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>