Jump to content

[SOLVED] menu not echoing out selected?


runnerjp

Recommended Posts

<select  id="gender" name="gender">
              <option value="Male" selected="<?php echo $getuserprofile['gender']; ?>" >Male</option>
              <option value="Female" selected="<?php echo $getuserprofile['gender']; ?>"  >Female</option>
            </select>

 

 

when i use <?php echo $getuserprofile['gender']; ?> on its own it shows  male as this is in the db... but when i use it like the code above it shows female :S

Link to comment
https://forums.phpfreaks.com/topic/145915-solved-menu-not-echoing-out-selected/
Share on other sites

Hi you misunderstand the html use of the selected option it is used as follows

 

<select name='gender' id='gender'>

  <option value='Male' selected>Male</option>

  <option value='Female'>Female</option>

</select>

 

notice how it is only the word selected and not selected='xxx'

what you must do is an if statement to echo selected into the tag when your value is true

 

$true=$getuserprofile['gender'];

<select name='gender' id='gender'>

  <option value='Male' <?php if ($true=='Male') {echo "selected";} ?>>Male</option>

  <option value='Female'<?php if ($true=='Female') {echo "selected";} ?>>Female</option>

</select>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.