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 13 of 13
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How to access an <option> number

    If I know the text of an option (or the value) how can I get the option position/number?

    For example:
    Code:
      <select name="numfontype">
    <option value="AARVC___.TTF">Aardvark Cafe</option>
    <option value="Abduction.ttf">Abduction</option>
    <option value="Adventure.ttf">Adventure</option>
    <option value="air_mitalic.ttf">Air Millhouse</option>
    </select>
    If I know 'Adventure' how can I return a 2 ???

    I can obtain the text and the values, but no positions. Please help!!!

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I cant understand ur post correctly,,
    but if u want that the drop down menu appear number wise then u
    can change ur options to resolve the series...

  • #3
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yeah, you'll have to sort of explain a bit more of what you're trying to accomplish.

    Is this even a server side problem? Or are you looking to use JavaScript and the DOM (or PHP and the DOM if you're generating this source at run-time) to get the index of a selected option? You're not quite clear, we could give a number of answers but I'm sure we'd rather give the correct one.
    Omnis mico antequam dominus Spookster!

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i can only speculate, but the sollution might be using an array to build the dropdown + to get the ordernumber (--> the key of the array).
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'm Sorry, it's a tricky situation.
    I have shopping cart software on site A and I want to add stuff from site B. Site B lets the user input options into fields to dynamically generate their visual product. The values from those fields are used for the product image and the PHP values are passed to hidden fields to send to the cart. For example:
    PHP Code:
    $numfontype $_POST['numfontype'
    <
    select name="numfontype"
    <
    option value="AARVC___.TTF">Aardvark Cafe</option
    <
    option value="Abduction.ttf">Abduction</option
    <
    option value="Adventure.ttf">Adventure</option
    <
    option value="air_mitalic.ttf">Air Millhouse</option
    </
    select
    The values need to be the font filename, but when I send the option to the cart it needs a number. The options in "numfontype" are in the same order as in the cart, but the cart starts at 433 and goes to 500. I know there is a way to save me hours of useless coding here. I'm just fuzzy/new to PHP.
    My final code would need to be:
    PHP Code:
    <input type="hidden" id="po104" name="product_options[104]" value"<?php echo($numfontype[$position# + 433]?>">
    I don't need to change the dropdown for the user, I just need to be able to find the number position of an option when I know the value or name. I'm not sure if I'm still being confusing?? My situation could also work if I can put the option position# in a PHP variable, but the value needs to stay as the font filename. Maybe I could have something like:
    PHP Code:
    $numfontype$_POST['numfontype']
    echo 
    $numfontype[$_POST['numfontype']]['name'];
    echo 
    $numfontype[$_POST['numfontype']]['name']['POSITION#???']; 
    Please let me know if it doesn't make sense. I'm not sure what I'm trying to say.
    THANKS THOUGH!!

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so when you build the dropdown, why don't you just set it to

    <select name="numfontype">
    <option value="434">Aardvark Cafe</option>
    <option value="435">Abduction</option>
    <option value="436">Adventure</option>
    <option value="437">Air Millhouse</option>
    </select>
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I need that value to generate an image of the product. I know this is getting messy. Let me try and ask this question:

    If I have This:
    PHP Code:
    $numfontype $_POST['numfontype']  
    <
    select name="numfontype">  
    <
    option value="AARVC___.TTF">Aardvark Cafe</option>  
    <
    option value="Abduction.ttf">Abduction</option>  
    <
    option value="Adventure.ttf">Adventure</option>  
    <
    option value="air_mitalic.ttf">Air Millhouse</option>  
    </
    select
    How do I finish this?
    PHP Code:
    echo $numfontype#Adventure.ttf
    echo $numfontype[$_POST['numfontype']]['name'];  #Adventure
    $fontname $numfontype[$_POST['numfontype']]['name'];  
    echo 
    $numfontype[$_POST['numfontype']]['$fontname']['POSITION#???']; #2, the position of the option 
    I want to use PHP but maybe this would be better done in Javascript?

  • #8
    Saz
    Saz is offline
    Regular Coder
    Join Date
    Jun 2005
    Posts
    277
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Anyone know if raf is still around ?

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    And you had to dredge up this old article to ask this?

  • #10
    Banned
    Join Date
    Feb 2008
    Location
    Winnipeg, Canada
    Posts
    396
    Thanks
    0
    Thanked 29 Times in 29 Posts
    I don't know why this is in PHP if you would use this at run time, but in JS here's a function that will return the number you're looking for if you pass it the string:

    Code:
    function get_position(option_name)
       for (var i=0;i<=document.getElementById("selectname").options.length;i++)
       {
          if (document.getElementById("selectname").options[i].value == option_name)
          {
             return (i + 1)
          }
       }
    }

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Did you read my response? This is an old article, I don't think you'll help anyone by solving the problem.

  • #12
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    If you use PHP, you'll have to pre-define the options that exist in the select, then iterate them and look for that value:
    PHP Code:
    $options = array(
        
    'AARVC___.TTF'    => 'Aardvark Cafe',
        
    'Abduction.ttf'   => 'Abduction',
        
    'Adventure.ttf'   => 'Adventure',
        
    'air_mitalic.ttf' => 'Air Millhouse',
    );

    $selected = isset( $_POST['numfontype'] ) ? $_POST['numfontype'] : NULL;

    $index 0;

    foreach ( 
    $options as $value => $text )
    {
        if ( 
    $value === $selected )
        {
            break;
        }
        
    $index++;
    }

    echo 
    $index
    Or, if you end up using JavaScript, have a look at selectedIndex,

  • #13
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by aedrin View Post
    And you had to dredge up this old article to ask this?
    ha ha

    i really not able to unserstand why did SAZ used this article to find his loving mate raf
    Please de-reputate me
    0o0o0o0

    Its better to rule in Hell then to serve in Heaven


  •  

    Posting Permissions

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