The <select> tag doesn't support a value option

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dkate777
    New Member
    • Jun 2007
    • 18

    The <select> tag doesn't support a value option

    I have a PHP/mySQL database running, and I've realized I'm come across an awkward little bug.

    I have a form where a user fills out their information using populated drop down boxes. This information gets stored in the database. Then, if a user wants to edit their information later, they can go to the Edit Details page which calls up a page almost identical to the original form when they first set themselves up. Of course, the person's information is pulled from the database, and is echoed as the values of the form elements.

    My issue is that the drop down boxes do not echo the value They are updated in the database correctly, but the drop downs don't know how to select the appropriate values.

    The <select> tag doesn't support a value option, and I can't hardcode the selected="selec ted" into any specific <option> tag because the input from the database needs to determine which one that is.

    Any suggestions anyone has would be greatly appreciated. Thanks!
  • sumaabey
    New Member
    • Jun 2007
    • 29

    #2
    Try this
    [code=php]<?
    $sql="select name from table name";
    execute the query
    $row=mysql_fetc h_array($res);
    ?>
    <select>
    <option value="<?=$row['name']?>" selected></option>
    </select>[/code]

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Originally posted by sumaabey
      Try this
      <?
      $sql="select name from table name";
      execute the query
      $row=mysql_fetc h_array($res);
      ?>
      <select>
      <option value="<?=$row['name']?>" selected></option>
      </select>
      Alternatively you can use selected="selec ted" in the option tag of the select element. This works just fine.

      So as you build up the select using your database you can add this line to the item that is stored against the user.

      Cheers
      nathj

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, dkate.

        [code=php]
        foreach($dbresu lts as $row => $data)
        echo "<option value=\"{$data['rowid']}\"" . (($data['rowid'] === $selectedValue)
        ? ' selected="selec ted"'
        : ''
        ) . ">{$data['rowname']}</option>\n";
        [/code]

        If that is not an option, you could add some JavaScript somewhere after you create the SELECT element that runs through the options for your SELECT and sets the selected property to true if its value matches.

        Comment

        • dkate777
          New Member
          • Jun 2007
          • 18

          #5
          Thanks All

          I run the test and i see it's selected the right value, but still not display it.
          Is anybody know why it's display?

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya dkate.

            Originally posted by dkate777
            I run the test and i see it's selected the right value, but still not display it.
            Is anybody know why it's display?
            Please elaborate. What do you mean by 'selected the right value', and 'still not display it'?

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Originally posted by dkate777
              Thanks All

              I run the test and i see it's selected the right value, but still not display it.
              Is anybody know why it's display?
              Perhaps you could post the relevant code?

              Comment

              • dkate777
                New Member
                • Jun 2007
                • 18

                #8
                OK, I know where is my problem is.
                I use jave script to display value on this form from other form
                document.form.f ieldname.value= firstformValue

                i can display this value on textbox, but not on select box. I think it's possible, but i don't know how to do this.
                Is anybody know how to pass value the right way

                Comment

                • Purple
                  Recognized Expert Contributor
                  • May 2007
                  • 404

                  #9
                  Hi dkate777,

                  you need to build the select statement with all of the options as you do on the initial form and append selected="selec ted"

                  take a look at the following function:

                  [PHP]//*
                  //* function - displays the job cost codes
                  //*
                  function job_cost_cat_se lect($data)
                  {
                  $code_select = "<tr><td>Co st type :</td><td><select name=\"jcexp_co de\">
                  <option value=\"0\" selected=\"sele cted\">Not selected</option>";
                  $rows = 0;
                  $count = count($data);
                  while($rows < $count)
                  {
                  if(isset($data[$rows][0])) $code_select = $code_select . "<option value=\"" .
                  rtrim($data[$rows][0]) . "\">" . rtrim($data[$rows][1]) . "</option>";
                  $rows++;
                  }
                  $code_select = $code_select . "</select></td></tr>";
                  if (isset($_POST['jcexp_code']))
                  {
                  $code_select = str_replace("<o ption value=\"".$_POS T['jcexp_code']."\">",
                  "<option value=\"".$_POS T['jcexp_code']."\" selected=\"sele cted\">",$code_ select);
                  $code_select = str_replace("<o ption value=\"0\" selected=\"sele cted\">",
                  "<option value=\"0\">",$ code_select);
                  }

                  return($code_se lect);
                  }[/PHP]

                  Hope this helps

                  Regards Purple

                  Comment

                  Working...