Jump to content

i am wanting the value to echo $gender in a drop down menu


runnerjp

Recommended Posts

im tryin to use

<select class="input" id="gender" name="gender"> 
<option value="<?php echo $gender ?>" selected="selected"></option>
<option value="Male" >Male</option>
<option value="Female" >Female</option>
</select> [code]

so the users gender they selected will show but it fails to do so ?

[/code]

Link to comment
Share on other sites

Think you'll need to echo it inside the option tags aswell (for it to show):

 

<select class="input" id="gender" name="gender"> 
<option value="<?php echo $gender ?>" selected="selected"><?php echo $gender ?></option>
<option value="Male" >Male</option>
<option value="Female" >Female</option>
</select>

 

But what's with the second and third option? Do you want the dropdown to automatic select the user specified gender stored in $gender, giving the user the ability to change it to the other option?

Link to comment
Share on other sites

what you are looking for is this:

 

<select class="input" id="gender" name="gender"> 
<option value="Male" <?php if($gender == 'Male') echo 'selected'; ?>>Male</option>
<option value="Female"  <?php if($gender == 'Female') echo 'selected'; ?>>Female</option>
</select>

Link to comment
Share on other sites

humm that worked grate lol so if i where to do same with

<select id="day" class="input" name="DateOfBirth_Day">
<option value="<?php echo $day ?>">- Day - </option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>

 

i would have to do

<option value="29" <?php if($day == '29') echo 'selected'; ?>>29</option>
<option value="30"<?php if($day == '30') echo 'selected'; ?>>30</option>
<option value="31"<?php if($day == '31') echo 'selected'; ?>>31</option>

 

or is there a faster way lol

Link to comment
Share on other sites

<select id="day" class="input" name="DateOfBirth_Day">
<?php
  for ($optVal=1; $optVal<32; $optVal++) {
    $selected = ($day==$optVal)?' selected="selected"':'';
    echo "<option value=\"{$optVal}\"{$selected}>" . printf('%02s',  $optVal) . "</option>";
  }
?>
</select>

Link to comment
Share on other sites

ok iv gone for bit more messing around and now have

<p>
        <label for="birthmonth">Birth Month:</label>
        <select name="birthmonth" id="birthmonth">
            <?php
for($i = 1; $i <= 12; $i++)
{
    echo ($i < 10) ? '<option value="0'.$i.'">0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>';
}
?>
        </select>
        <label for="birthday">Birth Day:</label>
        <select name="birthday" id="birthday">
            <?php
for($i = 1; $i <= 31; $i++)
{
    echo ($i < 10) ? '<option value="0'.$i.'">0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>';
}
?>
        </select>
        <label for="birthyear">Birth Year:</label>
        <select name="birthyear" id="birthyear">
            <?php
for($i = 1995; $i >= 1900; $i--)
{
    echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
        </select>
    </p>

 

but im unsure on how to display the current values selcted by the user in the db ??

Link to comment
Share on other sites

Change $prevSelectedbirthmonth withthe default value you want.

 

<?php
for($i = 1; $i <= 12; $i++)
{
    $selected = ($prevSelectedbirthmonth==$i)?' selected="selected"':'';
    echo ($i < 10) ? '<option value="0'.$i.'"'.$selected.'>0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>';
}
?>

Link to comment
Share on other sites

i did

 <label for="birthmonth">Birth Month:</label>
        <select name="birthmonth" id="birthmonth">
            <?php

for($i = 1; $i <= 12; $i++)
{
    $selected = ($month==$i)?' selected="selected"':'';
    echo ($i < 10) ? '<option value="0'.$i.'"'.$selected.'>0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>';
}
?>

        </select>
        <label for="birthday">Birth Day:</label>
        <select name="birthday" id="birthday">
            <?php
for($i = 1; $i <= 31; $i++)
{
    echo ($i < 10) ? '<option value="0'.$i.'">0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>';
}
?>
        </select>
        <label for="birthyear">Birth Year:</label>
        <select name="birthyear" id="birthyear">
            <?php
for($i = 1995; $i >= 1900; $i--)
{
    echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
        </select>
    </p></td>
</tr>

and did not work :(

Link to comment
Share on other sites

Didn't realize you already had an IF statemetn in that code. But, it would be very helpful if you explained what was happening instead of just saying "It didn't work"

 

<?php
for($i = 1; $i <= 12; $i++)
{
    $selected = ($month==$i)?' selected="selected"':'';
    echo "<option value=\"0$i\"$selected>0$i</option>";
}
?>

Link to comment
Share on other sites

Again, don't just state that "it didn't work" give specifics of what IS happening. Is the select list being generated successfully? If so, have you checked the generated HTML output to see if "selected" is in the code, but maybe not formatted correctly?

 

How are you setting the variable for the default value $month? Have you verified that it holds a value?

 

Were not mind readers. Perhaps the $month value has the leading '0' in which case '08' != '8'.

 

This is a very simple problem and should not need two pages on the forum to solve.

Link to comment
Share on other sites

hummm ok i have been thinkin on the subject lol and have relized a fault of mine.... why would someone need to chnage their birthday ??? lol as this will stay the same.. so all this trouble and its not really needed but i would still like to know where its going wrong so

 

if i select 07 as the date 09 month and 1995 as year then the output is 07-09-1995 that works fine in batabase and everything

 

but when i go back to the update field where i have an example of

 

<textarea class="input" id="about_me" name="about_me" rows="4" cols="40"><?php echo $about_me ?></textarea>

 

where the text allready added by the users is shown

 

but the date fields reset themselfs to 01-01-1995  i want it to show 07-09-1995

 

is that bttr ??

Link to comment
Share on other sites

Well, you still didn't answer my questions above as to whether you have done some of that debugging.

 

I would start from the beginning. First check the "default" values received from the query to make sure you are setting those values correctly. If they are check to see if they are being interpreted correctly in the comparison. If so, then determine what is wrong.

 

If I run this code, the select list is getting set to the correct default value.

<select name="month">

<?php

$month = '3';

for($i = 1; $i <= 12; $i++)
{
    $selected = ($month==$i)?' selected="selected"':'';
    echo ($i < 10) ? '<option value="0'.$i.'"'.$selected.'>0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>';
}

?>

</select>

 

So, your problem is most likely that $month is not getting set properly OR the value of $month has the zero padding on it. (e.g. '08' != '8')

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.