remember last <option> selected?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chaza
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2003 11:28 am

remember last <option> selected?

Post by chaza »

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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I'd suggest using JavaScript for this task.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

I think that setting a variable that contains the last item selected and using JavaScript to check the variable and set it as the selected item in the select box would work nicely.


John M
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Define refresh/reload.

After pressing the submit button, or by pressing F5/reload in the browser?

If the later, PHP will not work at all, as nothing is passed on to the server . If the former, no javascript is needed. More info available if so.
chaza
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2003 11:28 am

Post by chaza »

I mean by pressing the submit button.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Aha. The following might get you more ideas:

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>
I'm using the alternate form of if-then-else in the <input type="radio" ...> tags, to display CHECKED if the value is indeed the same as what was posted...

Hope that helped.
chaza
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2003 11:28 am

Post by chaza »

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>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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>
Let me translate your code in English:
Briefly, select all items if the year is equal to 1, otherwise select nothing.

did you intend to code exactly this?
Try the following:

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>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Weirdan looks better, but...

Code: Select all

if (!isset($_POST['year'])){ 
        $variable = $_POST['year'];
...is wrong.
If you read this, it translates: If Year is not set, set $variable to it, else set it as ''.

I'm using !empty() in my example, that has differences.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You're right, JAM, I missed the `!` sign.
Post Reply