Set the selected text for option box of a menu..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Advo

    Set the selected text for option box of a menu..

    Having major issues with this simple task, and I cant work out why its
    not doing as it should/expected.

    Basically, ive got a drop down box with the added bit of php:


    <select name="title" id="title">
    <?php
    if($_SESSION['title'] != "") {
    echo '<option value="'. $_SESSION['title'] .'"
    selected="selec ted"></option>';
    }
    else {
    echo "<option value=\"\" selected></option>";
    }
    ?>

    <option>Mr</option>
    <option>Mrs</option>
    <option>Miss</option>
    <option>Ms</option>
    <option>Dr</option>
    </select>


    Basically, if the user submits a form, the title is stored in the
    session, this works fine, and then when returned to the page, the value
    should be filled in.

    Yet, when they return to the page, there value they selected is not
    shown in this box, instead, theres just a gap. The source seems
    correct, and this is whats shown:

    <select name="title" id="title">
    <option value="Mr" selected="selec ted"></option>

    so it is working like that, but still not showing anything.

    Any ideas please?

  • Jonathan N. Little

    #2
    Re: Set the selected text for option box of a menu..

    Advo wrote:
    <snip>
    Yet, when they return to the page, there value they selected is not
    shown in this box, instead, theres just a gap. The source seems
    correct, and this is whats shown:
    >
    <select name="title" id="title">
    <option value="Mr" selected="selec ted"></option>
    >
    so it is working like that, but still not showing anything.
    Because you have made an HTML markup error with the OPTION element. You
    are missing the 'displayed' text...

    <option value="parsedVa lue">Displayed Value</option>

    Your code:
    <?php
    if($_SESSION['title'] != "") {
    echo '<option value="'. $_SESSION['title'] .'"
    selected="selec ted"></option>';
    }
    ...
    Needs to be:

    if($_SESSION['title'] != "") {
    echo '<option value="'. $_SESSION['title'] .'"selected="se lected">' .
    $_SESSION['title'] .'</option>'; }
    ....

    --
    Take care,

    Jonathan
    -------------------
    LITTLE WORKS STUDIO

    Comment

    • Tyrone Slothrop

      #3
      Re: Set the selected text for option box of a menu..

      On 19 Oct 2006 04:53:58 -0700, in comp.lang.php you wrote:
      >Having major issues with this simple task, and I cant work out why its
      >not doing as it should/expected.
      >
      >Basically, ive got a drop down box with the added bit of php:
      >
      >
      ><select name="title" id="title">
      ><?php
      >if($_SESSION['title'] != "") {
      > echo '<option value="'. $_SESSION['title'] .'"
      >selected="sele cted"></option>';
      > }
      >else {
      > echo "<option value=\"\" selected></option>";
      >}
      >?>
      >
      ><option>Mr</option>
      ><option>Mrs</option>
      ><option>Miss </option>
      ><option>Ms</option>
      ><option>Dr</option>
      </select>
      >
      >
      >Basically, if the user submits a form, the title is stored in the
      >session, this works fine, and then when returned to the page, the value
      >should be filled in.
      >
      >Yet, when they return to the page, there value they selected is not
      >shown in this box, instead, theres just a gap. The source seems
      >correct, and this is whats shown:
      >
      ><select name="title" id="title">
      ><option value="Mr" selected="selec ted"></option>
      >
      >so it is working like that, but still not showing anything.
      >
      >Any ideas please?
      It looks like your HTML syntax is the problem:
      <option value="Mr">Mr</option>

      I generally code selects like:
      <?
      $title_array = array ('Mr','Mrs','Mi ss','Ms','Dr');
      foreach ($title_array as $title) { ?>
      <option
      value=<?=$title ?>"<?=(in_array ($_SESSION['title'],$title_array)) ?'
      selected':''?>> <?=$title?></option>
      <? } ?>

      Comment

      Working...