<select name="header">                       
foreach(options_typography_get_google_fonts() as $face2) {
echo '<option id="fontface2"';
echo 'name="fontface2"'; 
echo 'value="';
echo $face2;
echo '"/>';
echo $face2;                
echo '</option>';
echo '<br />';
}
?>
</select>

I have stored the chosen value from the drop down list into a session. What I am trying to accomplish is "selecting" the option that was chosen and stored as the value after the form has been submitted.

I have tried adding this:

if($_SESSION['header']==$face2){ echo '"selected"'; }

And it works, however in the output - no matter where I put it - there isn't a space afterwards so the output is not showing selected. I have tried to use " ", ' ' and echo ' ' - but each one turns into an "=" sign in the output.

If anyone could help me - it would just mean the world! I have been playing with this for about 2 hours and I ready to pull my hair out!

Thanks!
Lynne

What does this do ?

<select name="header"> 
<?php
foreach(options_typography_get_google_fonts() as $face2) {
  echo "<option ";
  if ($_SESSION['header'] == $face2) 
    echo ' selected="selected"';

  echo ">$face2</option>";
}
?>
</select>

It populates a drop down box with an array of Google Fonts for a web form. I want to keep the value/session that is created after the form is submitted. It's an online DIY Wordpress template creator and when the users go back to try different fonts, they have to remember what they chose or start all over. I want to create "selected" so that the first font showing is what they submitted when they return after viewing their choices.

When I use this:

<select name="header">                         


                                    foreach(options_typography_get_google_fonts() as $face2) {

                                    echo '<option id="fontface2"';


                                    echo 'name="fontface2"';
                                    if($_SESSION['header']==$face2){ echo '"selected"'; }
                                    echo 'value="';
                                    echo $face2;

                                    echo '"></option>';
                                    echo $face2;                

                                    echo '</option>';
                                    echo '<br />';
                                    }
                                ?>
                                </select>  

The output is as follows - all I need is a simple space and nothing is working for me!

<select name="header">
<option id="fontface2" value="Rouge Script" name="fontface2">Rouge Script</option>
<option id="fontface2" "selected"value="Handlee" name="fontface2">Handlee</option>

I just need a space between "selected" and value.
I have tried:

echo ' ';
echo " ";
echo '&nbp';

But they turn into an = sign.

Yes, because you should not use quotes around selected, unless you output

selected="selected"

With quotes it is considered an attribute value, and for that an attribute name needs to be present, hence the equal sign.

Thank you! I have it outputting selected="selected" now - it's just grabbing the wrong value so now I have to figure that part out. This was driving me crazy!

if($_SESSION['header']==$face2){ echo 'selected ="selected"'; }

This isn't returning the correct value. Any ideas? Thanks so much for your help! I was looking at it too hard I think!

Actually - it is but you can't see it until it's refreshed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.