remember last <option> selected?
Posted: Wed Nov 05, 2003 6:52 am
Is there a way of remembering the last item selected in a <select> - option dropdown. So when the page refreshes/reloads the last thing selected is still in the select box?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<pre>
<?php
print_r($_POST); // Show what happens
if (!empty($_POST['foo'])) { // check to see if any of the radio's was used
$variable = $_POST['foo']; // ...if so, set the variable to it's value
} else {
$variable = ''; // or if not, make it blank.
}
?>
<form method="post">
<input type="radio" name="foo" value="1" <?php echo ($variable == 1 ? 'CHECKED ' : ''); ?>/>1
<input type="radio" name="foo" value="2" <?php echo ($variable == 2 ? 'CHECKED ' : ''); ?>/>2
<input type="submit" />
</form>Code: Select all
<?php
if (!isset($_POST['year'])){
$variable = $_POST['year'];
} else {
$variable = '';
}
$x = ($variable == 1)? ' SELECTED ' : '';
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" id="search">
<select name="year">
<?php
for ($k=1999;$k<=2004;$k++){
print "<option value='$k' $x>$k</option>";
}
?>
</select><br />
<input type="submit" value="search" />
</form>Let me translate your code in English:chaza wrote:I think that's what I want, but I can't seem to get it to work.
I am trying to use it like this:
Code: Select all
<?php if (!isset($_POST['year'])){ $variable = $_POST['year']; } else { $variable = ''; } $x = ($variable == 1)? ' SELECTED ' : ''; ?> <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" id="search"> <select name="year"> <?php for ($k=1999;$k<=2004;$k++){ print "<option value='$k' $x>$k</option>"; } ?> </select><br /> <input type="submit" value="search" /> </form>
Code: Select all
<?php
if (!isset($_POST['year'])){
$variable = $_POST['year'];
} else {
$variable = '';
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" id="search">
<select name="year">
<?php
for ($k=1999;$k<=2004;$k++){
print "<option value='$k' ".(($variable == $k)? ' SELECTED ' : '').">$k</option>";
}
?>
</select><br />
<input type="submit" value="search" />
</form>Code: Select all
if (!isset($_POST['year'])){
$variable = $_POST['year'];