show previously selected value in SELECT box?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
deBassMan
Forum Newbie
Posts: 4
Joined: Sun Aug 19, 2007 2:58 am

show previously selected value in SELECT box?

Post 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
RhapX
Forum Commoner
Posts: 30
Joined: Mon Dec 05, 2005 5:24 pm
Location: Seattle, Washington

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
User avatar
deBassMan
Forum Newbie
Posts: 4
Joined: Sun Aug 19, 2007 2:58 am

Post by deBassMan »

thanks both

Volka that was clear and concise

cheers
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

deBassMan wrote:thanks both

Volka that was clear and concise

cheers
Volka is always clear and concise! :)
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post 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 ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Jaxolotl: your script will throw a notice if $_POST['xyz'] is not defined.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post 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
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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"'
Post Reply