I am using the code below to populate a select list from fields in a db and it works great, the problem is how do I make it sticky? if the user types in the wrong info the page reloads and it does not remember what was selected from the list.
I have tried a few different if statements but i couldn't get it working. The list populates with 15 customer names, what would be the best way to tackle this? Thanks
<fieldset>
<legend><b>Check to see how many times a user has logged in</b></legend>
<p><label for="fname">First Name:</label><input type="text" id="fname" name="fname" value="<?php if (!empty($fname)) echo $fname; ?>" /></p>
<p><label for="ln开发者_高级运维ame">Last Name:</label><input type="text" id="lname" name="lname" value="<?php if (!empty($lname)) echo $lname; ?>" /></p>
<p><label for="customer">Customers:</label>
<select name="customer">
<?php
$custNames = getCustomers();
foreach($custNames as $customers){
echo '<option value="' . $customers . '">' . $customers . '</option>';
}
?>
</select>
</p>
<p class="submit"><input type="submit" value="See Logins" name="submit" /></p>
</fieldset>
You need an if
statement which prints a selected
attribute to the chosen element.
For example:
<?php
$custNames = getCustomers();
foreach($custNames as $customers){
$extra = '';
if($_POST['customer'] == $customers) {
$extra = 'selected="selected"';
}
echo '<option value="' . $customers . '" ' . $extra . '>' . $customers . '</option>';
}
?>
I'd say use cookies or store that information as session data. And then use the code posted by Emil Vikström above.
精彩评论