Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2003
    Location
    Houston, Texas
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP <select><option> problem

    Ok, here is my problem. I have a drop down menu where I select an option. and then submit the form. What I want to do is when I submit the form is tag the option as selected so it will display the
    value every time I submit the form unless I select another option
    and then I want to keep that value.

    Here is some basic code I have I written but for some reason
    it does not work. Please excuse the sloppy code, I threw this together in a hurry.

    <?php
    if($tt != "2")
    $veg="Avacado";
    ?>
    <html>
    <head><title></title></head>
    <?php
    echo"
    <p align='center'>
    <body>
    <form action='$PHP_SELF' method='get' name='my_form'>
    <select name='veggies'>";
    ?>
    <option value=<?php echo $veg; ?> <?php if($_POST['veggies'] == $veg) $slct= "selected"; ?>><?php echo $veg; ?></option>
    <?php
    echo "
    <option value='Lettuce', $slct>Lettuce</option>
    <option value='Corn'>Corn</option>
    <option value='Tomato'>Tomato</option>
    <option value='Spinach'>Spinach</option>
    <option value='Broccoli'>Broccoli</option>
    </select><br><br>
    <input type='submit' value='Submit'>
    <input type='hidden' name='tt' value='2'>
    <input type='hidden' name='veg' value='$veggies'><br><br>
    </p?";
    ?>

    </form>
    </body>
    </html>

    This is what is happening:
    The first time I load the page, the "Avacado"
    option is displayed.

    Now I select the option "Tomato" and
    click on the submit button.

    Now the "Lettuce" option is displayed.

    I now click the submit button again
    and the "Tomato" option is displayed.

    I now click the submit button again
    and the "Lettuce" option is displayed.

    I now click the submit button again
    and the "Tomato" option is displayed.

    Every time I click on the submit button, the options keep flip-flopping between "Lettuce" and "Tomato". I want the
    "Tomato" option to stay on the screen unless I
    select another option then I want that option to remain on the screen until I change it.

    Could someone please help me and tell me what I am doing
    wrong here.

    Thank you.
    Beyond a critical point within a finite space, freedom diminishes as numbers increase. ...The human question is not how many can possibly survive within the system, but what kind of existence is possible for those who do survive."

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It's as simple as this

    PHP Code:


    <?php
    $item 
    $_POST["item"];
    ?>

    <select name="item"> 
    <option <?php if($item == "item1") echo "selected"?> value="item1">Item 1</option> 
    <option <?php if($item == "item2") echo "selected"?> value="item2">Item 2</option> 
    </select>
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whatever is between the <option></option> is the value. i.e if I had
    <select name="words">
    <option>Hello</option>
    <option>Hello World!</option>
    </select>
    and you hit submit with "Hello World" selected, then $_POST['words'] would have a value of Hello World!
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    New Coder
    Join Date
    Jul 2003
    Location
    Houston, Texas
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a million guys. I couldn't have done it without you. You saved the day for me. This works perfectly now.
    Beyond a critical point within a finite space, freedom diminishes as numbers increase. ...The human question is not how many can possibly survive within the system, but what kind of existence is possible for those who do survive."

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by SDP2006
    Whatever is between the <option></option> is the value. i.e if I had
    <select name="words">
    <option>Hello</option>
    <option>Hello World!</option>
    </select>
    and you hit submit with "Hello World" selected, then $_POST['words'] would have a value of Hello World!
    You need to lay off the crack pipe.

    You need to specify the value using the value attribute.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh hah hah
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Originally posted by Spookster
    You need to lay off the crack pipe.
    hahaha Best laugh I've had in ages


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •