Page 1 of 1

show previously selected value in SELECT box?

Posted: Sun Aug 19, 2007 3:08 am
by deBassMan
Hi all

I have a form that accepts user input from a variety of TEXT and SELECT boxes and save the values in a mySQL table - OK so far.

I need to offer a way of subsequently enabling the user to update the values.

Text box values are no problem - but what is the best way of displaying a form with the values of SELECT boxes displayed?

maybe I'm missing something ...

thanks in advance for your time

Posted: Sun Aug 19, 2007 3:41 am
by RhapX
If you could post a link to the form you're using, I will write out a script with proper variables to fit the occasion. It's late here so I will check when I get up.

Posted: Sun Aug 19, 2007 4:26 am
by volka
The option element having an attribute selected="selected" will be the ...err.. selected option ;)
e.g.

Code: Select all

<html>
  <head><title>...</title></head>
  <body>
    <form method="post" action="?">
      <div>
        <select name="xyz">
          <option>1</option>
          <option selected="selected">2</option>
          <option>3</option>
        </select>
        <input type="submit" />
      </div>
    </form>
  </body>
</html>
will pre-select option #2 (instead of #1)

Your php script has to print the option tag you want to be selected with this attribute, e.g.

Code: Select all

<html>
  <head><title>...</title></head>
  <body>
    <form method="post" action="?">
      <div>
        <select name="xyz"><?php
          for($i=0; $i<5; $i++) {
            if ( $i==$selectedValue ) {
              echo '<option selected="selected">', $i, '</option>';
            }
            else {
              echo '<option>', $i, '</option>';
            }
          }
        ?></select>
        <input type="submit" />
    </div>
    </form>
  </body>
</html>

Posted: Sun Aug 19, 2007 8:58 am
by deBassMan
thanks both

Volka that was clear and concise

cheers

Posted: Sun Aug 19, 2007 10:50 pm
by califdon
deBassMan wrote:thanks both

Volka that was clear and concise

cheers
Volka is always clear and concise! :)

Posted: Mon Aug 20, 2007 10:33 am
by Jaxolotl
I made this simple function to call everytime I need it

Code: Select all

#############################################
###   Returns SELECTED if option value == $_POST['value']
#  
  //  $id_string       -> (string)
  //  $id_posted       -> (string)
  
  function is_selected_option($id_string,$id_posted){
    if($id_string == $id_posted){
      return " selected ";
    }
  }
Using it with your code you may script this

Code: Select all

<html>
  <head><title>...</title></head>
  <body>
    <form method="post" action="?">
      <div>
        <select name="xyz">
          <option value="1" <?php echo is_selected_option("1",$_POST['xyz']);?>>1</option>
          <option  value="2" <?php echo is_selected_option("2",$_POST['xyz']);?>>2</option>
          <option value="3" <?php echo is_selected_option("3",$_POST['xyz']);?>>3</option>
        </select>
        <input type="submit" />
      </div>
    </form>
  </body>
</html>
Is very simple but usefull. Ofcourse it could be writtend better ;)

Posted: Mon Aug 20, 2007 10:43 am
by John Cartwright
Jaxolotl: your script will throw a notice if $_POST['xyz'] is not defined.

Posted: Mon Aug 20, 2007 5:08 pm
by Jaxolotl
Jcart wrote:Jaxolotl: your script will throw a notice if $_POST['xyz'] is not defined.
I'm forword improving all my script library because of that kind of stupid errors I left behind. I post this just to suggest deBassMan to start writing functions for this kind of common use piece of code so you don't have to write them again and again, and in cases like mine (mean bug fixing like the bug you notice) if you have a a code library you fix it one time only without loosing time looking for the mistake all over the code-set. Ass soon as you understand the need of abstractions, as less time you loose.

by the way thank you very much Jcart for your suggestion ;) - it comes just in the moment I was working on such bad behaviours I use to have

Posted: Mon Aug 20, 2007 5:29 pm
by Jaxolotl
Think this is better or have other suggestions?

Code: Select all

<?php
############################################# 
###   Returns SELECTED if option value == $_POST['value']
#  
  //  $value_string       -> (string)
  //  $id_posted           -> (string) THE KEY OF THE POST ARRAY we want to retrive value
  
  function is_selected_option($value_string ,$id_posted){
    if((isset($_POST[$id_posted]))&&($value_string  == $_POST[$id_posted])){
      return " selected ";
    }
  }
  ?>
<html>
  <head><title>...</title></head>
  <body>
    <form method="post" action="?">
      <div>
        <select name="xyz" action="">
          <option value="1" <?php echo is_selected_option("1","xyz")?>>1</option>
          <option  value="2" <?php echo is_selected_option("2","xyz")?>>2</option>
          <option value="3" <?php echo is_selected_option("3","xyz")?>>3</option>
        </select>
        <input type="submit" />
      </div>
    </form>
  </body>
</html>

Posted: Mon Aug 20, 2007 5:31 pm
by feyd
You may want to be able to specify the array it should look at. Also, for standards compliance it should echo ' selected="selected"'