user2981393 user2981393 - 4 months ago 11
PHP Question

Default no selection for HTML <select> that is created in a for loop

My HTML select statement is:

<p>Height w/o shoes between (inches)</p>
<select name="woShoes">
<?php for ($i = 1; $i < 300; $i++) {?>
<option value="<?php echo $i;?>"><?php echo $i; ?></option>
<?php } ?>
</select> And
<select name="woShoes2">
<?php for ($i = 1; $i < 300; $i++) {?>
<option value="<?php echo $i;?>"><?php echo $i; ?></option>
<?php } ?>
</select>


But I want to add a default no selection since I am checking if the statement is selected or not, right now it's always 1 so it's processed as selected even though that is the default.

The way I check if it's empty is

if (!empty($_POST['woShoes']) && !empty($_POST['woShoes2'])) {


how can I add a default no selection select when I generate the values in a for loop?

Answer
<p>Height w/o shoes between (inches)</p>
<select name="woShoes">
<option selected="selected"></option>
<?php for ($i = 1; $i < 300; $i++) {?>
<option value="<?php echo $i;?>"><?php echo $i; ?></option>
<?php } ?>
</select> And
<select name="woShoes2">
<option selected="selected"></option>
<?php for ($i = 1; $i < 300; $i++) {?>
<option value="<?php echo $i;?>"><?php echo $i; ?></option>
<?php } ?>
</select>

You can have something like above where you put an empty selected value. For more about select attribute check here