I want to presist (=keep values if error on form) my values from a dropdown menu , this is what i started with :
...
$priorities = array('low','normal','high');
...
<select name="priority" id="priority">
<?php
foreach ($priorities as $pro){
echo '<option value="'.$pro.'">'.$pro.'</option>';
}
?>
</select>
It does the job but does not presist.
Now i want to get to something like this :
<select name="priority" id="priority">
<?php
$tel = 0;
foreach ($priorities as $pro){
echo '<option value="'.$tel.'"'.htmlentities('<?php if (isset($_POST[\'priority\']) && (int) $_POST[\'priority\'] === tel) { echo \'selected="selected"\'; } ?>').' >'.$pro.'</option>';
$tel++;
}
?>
</select>
But that of course gives an error.
Anyone has any suggestions thx
Here’s how I often implement this:
Just thought I’d point out you can actually shorten the ternary here like so
Sorry I’ve got a bit of a thing for creating the shortest code possible 🙂