By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

Edit Drop down box values

100+
P: 250
I want to edit database values using my web application. So first i load data to test boxex and drop down list. But drop down list only contain one value. I want to put all posible value to that and should high light particular one. (Because if user want to edit drop down box value)How can i do that?
Feb 13 '08 #1
Share this Question
Share on Google+
4 Replies


harshmaul
Expert 100+
P: 490
You will need to fill the drop down list with all the values, and then print selected in the drop down list if the value in the DB matches the one in the Drop Down list.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $valueFromDB = "apples";
  3. ?>
  4. <select>
  5. <?php
  6. $arr = array("please select", "apples", "pears", "grapes");
  7. foreach ($arr as $value) {
  8.     $selected= $value == $valueFromDB ? "selected" : "";
  9. ?>
  10. <option <?php echo $selected; ?>><?php echo $value; ?></option> 
  11. <?php
  12. }
  13. ?> 
  14. </select>
Feb 14 '08 #2

100+
P: 250
It is working ! Thanks
Feb 15 '08 #3

harshmaul
Expert 100+
P: 490
No worries. I'm here to help!
Feb 15 '08 #4

P: n/a
i tried ur code above.. it works for me to insert but it didn't retrieve the exact info from database.. what i mean is it only show the last dropdown value only

below is my code

<?php
$valueFromDB = "E1";
$valueFromDB = "E2";
$valueFromDB = "TM";
$valueFromDB = "TP";
?>
<select name="positiontype">
<?php
$arr = array("Select One", "E1","E2", "TM", "TP");
foreach ($arr as $row_rsXDsecondary['positiontype']){
// $row_rsXDsecondary['positiontype'] = $value;
$selected= $row_rsXDsecondary['positiontype'] == $valueFromDB ? "selected" : "";
?>
<option <?php echo $selected; ?>><?php echo $row_rsXDsecondary['positiontype']; ?></option>

}
<?php
}
?>
</select>
and it always show TP

thank you in advance
Oct 14 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.