Combox data selecttion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kallol123
    New Member
    • Nov 2009
    • 1

    Combox data selecttion

    I have a combox which contains the following values.

    Code:
    <select name="testType" id="testType">
    <option Value="1">As Per Spec</option>
    <option Value="2">O.K.</option>
    <option Value="3">Normal</option>
    <option Value="4">Satisfactory</option>
    <option Value="5">Good</option>
    <option Value="6">N.A.</option>

    Form mySql query I got value 'Good'.
    My problem how get selected the text 'Good' of combobox.

    Pls help asap.

    Kallol
    Last edited by Atli; Nov 20 '09, 10:44 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You mean; you want the initial value of the <select> to be set according to a value retrieved from a database?

    This is the general idea behind such code. You would of course have to tailor it to your own needs.
    [code=php]
    <?php
    // This would come from your Database
    $inital = 'Good';

    // The options you want printed.
    $options = array(
    1 => 'As Per Spec',
    2 => 'O.K',
    /* etc ... */
    5 => 'Good',
    6 => 'N.A.'
    );

    // Print the <select> box header.
    echo '<select name="testType" id="testType">' ;

    // Print all the options.
    foreach($option s as $_index => $_value)
    {
    // See if this is supposed to be the default
    // option and add the "selected" attribute
    // if it is.
    $selected = '';
    if($_value == $initial)
    {
    $selected = ' selected="selec ted"';
    }

    // Print the option
    // Note, the $selected variable will be empty
    // unless this is the selected option.
    echo "<option value=\"{$_inde x}\"{$selected} >{$_value}</option>";
    }

    // Close the select box.
    echo '</select>';

    ?>
    [/code]

    Comment

    Working...