Skip to content
Advertisement

Is there a more elegant way of remembering user input after submission for drop-down lists in a HTML form?

I have a number field and a drop-down list that I am using as part of a form, as follows:

<label for="height">Height/Thickness</label>:
<input <?php if (empty($messages) == false) {echo 'value="'.htmlentities($_POST['height'], ENT_QUOTES).'"';} ?> type="number" name="height" id="height" />
                <select class="dimensions" name="heightunit">
                    <option value="">Select...</option>
                    <option value="mm">mm</option>
                    <option value="cm">cm</option>
                    <option value="m">m</option>
                    <option value="in">in</option>
                </select>

The $messages variable is an array that stores any error messages generated by the form. The bit between the <?php & ?> tags are there to ensure that if there is an error, that it will echo back the values the user entered before they submitted, so saving them time (and frustration) from entering the data again.

I want to do the same with the drop down list. At the moment, I am adding the following line within each option tag (similar to this answer):

<?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'mm') {echo 'selected="selected"';} ?>

But then it’s really untidy:

....
<select class="dimensions" name="lengthunit">
    <option value="" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == '') {echo 'selected="selected"';} ?>>Select...</option>
    <option value="mm" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'mm') {echo 'selected="selected"';} ?>>mm</option>
    <option value="cm" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'cm') {echo 'selected="selected"';} ?>>cm</option>
    <option value="m" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'm') {echo 'selected="selected"';} ?>>m</option>
    <option value="in" <?php if ((empty($messages) == false) && ($_POST['lengthunit'] == 'in') {echo 'selected="selected"';} ?>>in</option>
</select>

My question is – Is there a more elegant way to do this, without adding the php tag inside each option tag?

(I am thinking along the lines of: if (OPTION_VALUE === $_POST['heightunit']) {echo 'selected="selected"';}. Is there anything that can be substituted for the OPTION_VALUE?)

EDIT: The reason I want to do this is because I want to apply this to a list of countries, which is over 200, and I just want to write ‘good code’.

**EDIT: Looking back at it now, it looks so easy…anyways, thank you for your help and this is what I went with in the end:

function lengthlist($selectname) {
    $distanceunits = array('','mm','cm','m','in');
    foreach ($distanceunits as $value) {
    if ($value == '') {$value = '...';}
        if ($value == $_POST["$selectname"]) {
            echo "<option value="$value" selected="selected">$value</option>";
        } else {
            echo "<option value="$value">$value</option>";
        }
    }
}

Advertisement

Answer

Or you can also load the dimensions values in an array and then just write them on a while loop

 <select class="dimensions" name="lengthunit">
<option>Select...</option>
<?php 
 $i=0;
 $array=array("mm", "cm", "m", "in");
while ($i<4){ ?>
<option value="<?php echo $array[$i];?>"<?php if ((empty($messages) == false) && 
($_POST['lengthunit'] == $array[$i])) {echo 'selected="selected"';} ?>><?php echo 
$array[$i];?></option>
<?php $i++; } ?>

</select>

Because in other cases you might wish to add more options for the users to select, so you would just add them to the array. You can also use a foreach loop instead of a while

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement