Value of selected item

Discussion in 'PHP' started by lost, Nov 29, 2005.

  1. #1
    How would i retrieve the value from the selection box.
    I have this line of code that is retrieving the index of the selected item, i need the value:

        
    $partno = $_POST['secondChoice'];
    
    Code (markup):

     
    lost, Nov 29, 2005 IP
  2. FFMG

    FFMG Well-Known Member

    Messages:
    1,091
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You need to set a value in your option and that is what will be set for your $_POST value. Something like…

    
    <SELECT NAME="pizzasize">
    <OPTION VALUE="s">small
    <OPTION VALUE="m">medium
    <OPTION VALUE="l">large
    </SELECT>
    
    Code (markup):
    Is that what you mean?

    FFMG
     
    FFMG, Nov 29, 2005 IP
  3. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    not really
    below you will see my selection box
    
      <SELECT NAME="firstChoice" ID="firstChoice" ONCHANGE="selectChange(this, genericform.secondChoice, arrItems1, arrItemsGrp1);"> 
      <OPTION VALUE="99"   selected <?php if(@strcmp($_POST['firstChoice'], 'Select Unit')  == 0) echo "SELECTED";?>> Select Unit   </OPTION> 
      <OPTION VALUE="0"            <?php if(@strcmp($_POST['firstChoice'], 'SDC1-2')       == 0) echo "SELECTED";?>> SDC1-2        </OPTION>
      <OPTION VALUE="1"            <?php if(@strcmp($_POST['firstChoice'], 'P73 Kit')      == 0) echo "SELECTED";?>> P73 Kit       </OPTION>
      <OPTION VALUE="2"            <?php if(@strcmp($_POST['firstChoice'], 'MDR2-2')       == 0) echo "SELECTED";?>> MDR2-2        </OPTION>
      <OPTION VALUE="3"            <?php if(@strcmp($_POST['firstChoice'], 'CSD1-3')       == 0) echo "SELECTED";?>> CSD1-3        </OPTION>
      <OPTION VALUE="4"            <?php if(@strcmp($_POST['firstChoice'], 'CSD1-4')       == 0) echo "SELECTED";?>> CSD1-4        </OPTION>
      <OPTION VALUE="5"            <?php if(@strcmp($_POST['firstChoice'], 'MLS1-1')       == 0) echo "SELECTED";?>> MLS1-1        </OPTION>
    
    Code (markup):
    what i'm trying to figure out is when the user selects one of those options from the selection box, for example P73 Kit, that i can get the value "P73 Kit" and not the index as i was getting with using this line of code:
    
    $partno = $_POST['secondChoice'];
    
    Code (markup):
     
    lost, Nov 30, 2005 IP
  4. FFMG

    FFMG Well-Known Member

    Messages:
    1,091
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Why not use an array?

    
    <?php
    $values = array( "Value A" => "Text A", 
    				 "Value B" => "Text B", 
    				 "Value C" => "Text C" );
    ?>
    <SELECT NAME="firstChoice" ID="firstChoice" ONCHANGE="selectChange(this, genericform.secondChoice, arrItems1, arrItemsGrp1);"> 				 
    <?php
    // the selected item, or "" if nothing selected.
    $choice = isset($_POST['firstChoice'])?$_POST['firstChoice']:"";
    
    // loop around each value/text in the array.
    foreach ( $values as $value => $text )
    {
    	$sel = (@strcmp($choice, $value)  == 0)? " SELECTED":"";
    
    	print( "<OPTION VALUE=\"".$value."\"".$sel.">".$text."</OPTION>" );
    }
    ?>
    
    Code (markup):
    So that, with the value passed, you can either compare the value or use the text, you get both.

    Also that way all you need to do is update the array and everything else will be updated.

    FFMG
     
    FFMG, Nov 30, 2005 IP
  5. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok thanks guys.
     
    lost, Nov 30, 2005 IP