2

I have a select tag ...

<select id="input_O6" type="text" name="select_input">
        <option value="Level 1">Level 1</option>
        <option value="Level 2">Level 2</option>
        <option value="Level 3">Level 3</option>
        <option value="Level 4">Level 4</option>
        <option value="Level 5">Level 5</option>
        <option value="Level 6">Level 6</option>
        <option value="Level 7">Level 7</option>
        <option value="Level 8">Level 8</option>
        </select>

the user selects a value and I store the selected value to the sql database. When I reload the page, I'm retrieving the value selected in my php and I'm trying to set the select tag to this value. Other inputs, I simply do it like this...

<input id="input_O3" type="number" value=<?php echo $storedValue_O3;?>>

Is there a simple way to do this for the select tag?

Thanks in advance

1
  • you have to check every value in that select and check if its same then add a selected="selected" to the option Commented Nov 26, 2014 at 7:29

3 Answers 3

1
for($i = 1; $i<=8; $i++){

  $selected = ($storedValue_O3 == $i) ? "selected" : null;

  echo "<option value='Level ".$i."' ".$selected.">Level ".$i."</option> ";

}
Sign up to request clarification or add additional context in comments.

1 Comment

Rather than just posting code, explain how/why it works.
0

An option field has a selected attribute. You can set this using php:

<select id="input_O6" type="text" name="select_input">
    <option value="Level 1" <?php echo ($selectedval == 1 ? 'selected' : '') ?>>Level 1</option>
    <option value="Level 2" <?php echo ($selectedval == 2 ? 'selected' : '') ?>>Level 2</option>
    <option value="Level 3" <?php echo ($selectedval == 3 ? 'selected' : '') ?>>Level 3</option>
    <option value="Level 4" <?php echo ($selectedval == 4 ? 'selected' : '') ?>>Level 4</option>
</select>

1 Comment

Thanks... this does just what I need!
0

Get the input variable From $_POST or $_GET depending upon your form submission method.

Get all the drop down options in an array, loop over them.

And create options.

If you get the selected value, make the particular option selected, otherwise, not selected.

This should work.

<?php
$select_input = ! empty($_POST['select_input']) ? $_POST['select_input'] : '';

//Get the input variable:

?>

And Do the following changes:

<?php
$select_input = ! empty($_POST['select_input']) ? $_POST['select_input'] : '';
$arrInp = array();
$arrInp['Level 1'] = 'Level 1';
$arrInp['Level 2'] = 'Level 2';
$arrInp['Level 3'] = 'Level 3';
$arrInp['Level 4'] = 'Level 4';

$arrInp['Level 5'] = 'Level 5';
$arrInp['Level 6'] = 'Level 6';
$arrInp['Level 7'] = 'Level 7';
$arrInp['Level 8'] = 'Level 8';
?>


<select id="input_O6" type="text" name="select_input">
    <?php
    foreach ($arrInp as $k => $v) {
        $selected = ($select_input == $k) ? 'selected="selected"' : '';
    ?>
        <option value="<?php echo $k;?>" <?php echo $selected;?>><?php echo $v;?></option>
        <?php
 } 
       ?>
</select>

1 Comment

you have mistyped $_POST['select_input'] multiple times, plus the OP explained he is getting his values from a SQL query - not a POST/GET request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.