Retrieving from <option>

Discussion in 'PHP' started by blueparukia, Oct 6, 2007.

  1. #1
    OK, I have a HTML option box with roughly 500 options, and I want to make 2 PHP variables from the optons. This is hard to explain, so I'll just show you:

    
    <option value="1">James</option>
    
    HTML:
    So from that example I want to get 2 variables:

    $name and $code

    $code should equal "1" (the value) and $name should equal "James" (the displayed option).

    I know how to use POST to get the value, but not the text the option displays.

    Thanks,

    BP
     
    blueparukia, Oct 6, 2007 IP
  2. ravish_83

    ravish_83 Peon

    Messages:
    221
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    here is trick to do this

    make your form like this

    
    <select name="pizza">
    <option value="1***James" >James</option> 
    .
    .
    .
    </select>
    
    HTML:
    and then do use the explode statement to separate them on the next page

    $pieces = explode("***", $pizza);
    PHP:
    now you have two values

    $pieces[0] = 1;
    $pieces[1] = James;
    PHP:
     
    ravish_83, Oct 6, 2007 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Thanks for the help, but I would rather not have to change 500 different options to make it like that. And the PHP code already has the variables $code and $name defined, so I would rather use those 2, instead of using $code[0], $code[1]/

    However, if their is no other alternative, I will do it like this,

    Thanks,

    BP
     
    blueparukia, Oct 6, 2007 IP
  4. roosevelt

    roosevelt Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    You cannot get the text the option displays. You have to use the method ravish showed or in your PHP code you will need a condition. Like if $code == 1 then $name = "James";
     
    roosevelt, Oct 7, 2007 IP
  5. bdude

    bdude Peon

    Messages:
    124
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could create a key eg:
    
    $key[1] = 'James';
    $key[2] = 'Jim';
    
    PHP:
    or
    
    $key = array('James', 'Jim', ...);
    
    PHP:
     
    bdude, Oct 7, 2007 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Thanks all, I will do what Ravish suggested, as it is the easiest way to do it,

    Thanks,

    BP
     
    blueparukia, Oct 7, 2007 IP