We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,099 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to save value from drop menu by using php.

how to save value from drop menu by using php.

<select name = 'color'>
    <option value='black'>Black</option>
    <option value='gray'>Gray</option>
    <option value='white'>White</option>
</select>

i am saving the value from text field by doing this. i am thinking if i can do some thing like this in drop down menu.

<input type="text" name="username"  
   value="<?php if(isset($_POST['username'])){echo htmlentities($_POST['username']);}?>" />
2
Contributors
1
Reply
1 Hour
Discussion Span
4 Months Ago
Last Updated
2
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

One approach:

$options = '';
$select_array = array('black', 'gray', 'white');
$sel_color = (isset($_POST['color']) && in_array($_POST['color'], $select_array)) ? $_POST['color'] : '';
foreach($select_array as $color){
    $sel = ($sel_color == $color) : ' selected="selected"' : '';
    $options .= "<option value='color'$sel>" . strtoupper($color) . "</option>"; 
}
...
<select name="color">
    <?php echo $options;?>
</select>
diafol
Keep Smiling
Moderator
10,653 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0564 seconds using 2.66MB