Jump to content

Recommended Posts

Hi all,

 

I have a very long list in my select option. Have a look:

<option value="2010">2010</option> 
<option value="2009">2009</option> 
<option value="2008">2008</option> 
<option value="2007">2007</option> 
<option value="2006">2006</option> 
<option value="2005">2005</option> 
<option value="2004">2004</option> 
<option value="2003">2003</option> 
<option value="2002">2002</option> 
<option value="2001">2001</option> 
<option value="2000">2000</option> 
<option value="1999">1999</option> 
<option value="1998">1998</option> 
<option value="1997">1997</option> 
<option value="1996">1996</option> 
<option value="1995">1995</option> 
<option value="1994">1994</option> 
<option value="1993">1993</option> 
<option value="1992">1992</option> 
<option value="1991">1991</option> 
<option value="1990">1990</option> 
<option value="1989">1989</option> 
<option value="1988">1988</option> 
<option value="1987">1987</option> 
<option value="1986">1986</option> 
<option value="1985">1985</option> 
<option value="1984">1984</option> 
<option value="1983">1983</option> 
<option value="1982">1982</option> 
<option value="1981">1981</option> 
<option value="1980">1980</option> 
<option value="1979">1979</option> 
<option value="1978">1978</option> 
<option value="1977">1977</option> 
<option value="1976">1976</option> 
<option value="1975">1975</option> 
<option value="1974">1974</option> 
<option value="1973">1973</option> 
<option value="1972">1972</option> 
<option value="1971">1971</option> 
<option value="1970">1970</option> 
<option value="1969">1969</option> 
<option value="1968">1968</option> 
<option value="1967">1967</option> 
<option value="1966">1966</option> 
<option value="1965">1965</option> 
<option value="1964">1964</option> 
<option value="1963">1963</option> 
<option value="1962">1962</option> 
<option value="1961">1961</option> 
<option value="1960">1960</option> 
<option value="1959">1959</option> 
<option value="1958">1958</option> 
<option value="1957">1957</option> 
<option value="1956">1956</option> 
<option value="1955">1955</option> 
<option value="1954">1954</option> 
<option value="1953">1953</option> 
<option value="1952">1952</option> 
<option value="1951">1951</option> 
<option value="1950">1950</option> 
<option value="1949">1949</option> 
<option value="1948">1948</option> 
<option value="1947">1947</option> 
<option value="1946">1946</option> 
<option value="1945">1945</option> 
<option value="1944">1944</option> 
<option value="1943">1943</option> 
<option value="1942">1942</option> 
<option value="1941">1941</option> 
<option value="1940">1940</option> 
<option value="1939">1939</option> 
<option value="1938">1938</option> 
<option value="1937">1937</option> 
<option value="1936">1936</option> 
<option value="1935">1935</option> 
<option value="1934">1934</option> 
<option value="1933">1933</option> 
<option value="1932">1932</option> 
<option value="1931">1931</option> 
<option value="1930">1930</option> 
<option value="1929">1929</option> 
<option value="1928">1928</option> 
<option value="1927">1927</option> 
<option value="1926">1926</option> 
<option value="1925">1925</option> 
<option value="1924">1924</option> 
<option value="1923">1923</option> 
<option value="1922">1922</option> 
<option value="1921">1921</option> 
<option value="1920">1920</option> 
<option value="1919">1919</option> 
<option value="1918">1918</option> 
<option value="1917">1917</option> 
<option value="1916">1916</option> 
<option value="1915">1915</option> 
<option value="1914">1914</option> 
<option value="1913">1913</option> 
<option value="1912">1912</option> 
<option value="1911">1911</option> 
<option value="1910">1910</option> 
<option value="1909">1909</option> 
<option value="1908">1908</option> 
<option value="1907">1907</option> 
<option value="1906">1906</option> 
<option value="1905">1905</option> 
<option value="1904">1904</option> 
<option value="1903">1903</option> 
<option value="1902">1902</option> 
<option value="1901">1901</option> 
<option value="1900">1900</option>

 

 

How do I retain the one selected by a user should in case there is an error in the registration form. I do know that something like this could work:

 

<?php if ($_POST['dob_year']=="1900") echo "selected='selected'"; ?>

 

which other method can I use in achieving this

Link to comment
https://forums.phpfreaks.com/topic/199713-remember-select-menu-selected/
Share on other sites

It'd be easier for you to do a loop when making the list in php.

 

for($i = 2010; $i > 1900; $i--)
{
     ?>
         <option value="<?php echo $i ?>" <?php if($_POST['dob_year'] == $i) echo 'selected="selected"' ?>><?php echo $i ?></option>

     <?php
}

 

Since this is a date thing, you could define the upper and lower limits using a time function and grabbing the year for the upper limit, and say take off ~150 for the lower limit:

$upper = intval(strftime('%Y'));
$lower = $upper - 150;
for($i = $upper; $i > $lower; $i--) {
    ....

I just tried it myself using

<form method="post" action="">
<select name="dob_year">
<?php


$upper = intval(strftime('%Y'));
$lower = $upper - 150;


$_POST['dob_year'] = empty($_POST['dob_year']) ? $upper : $_POST['dob_year'];

for($i = $upper; $i > $lower; $i--) {
?>
 <option value="<?php echo $i ?>" <?php if($_POST['dob_year'] == $i) echo 'selected="selected"' ?>><?php echo $i ?></option>

<?php
}

?>

</select>
<input type="submit" name="submit" value="submit" />
</form>

 

And it works fine.

 

Can you post your code so I can see where the problem could be?

Your code works fine but am encountering this problem. Instead of Year 2000 to be selected as default, how can I make this

<option value="0" selected="selected">Year</option>

 

to be the default selected and still remember what the user choose should in case there is an incomplete form filled. I hope you get my point here.

 

Ah. Something like this would work:

 

<?php $_POST['dob_year'] = empty($_POST['dob_year']) ? '': $_POST['dob_year']; ?>
<form method="post" action="">
<select name="dob_year">
<option value="0"  <?php if($_POST['dob_year'] == '') echo 'selected="selected"' ?>>Year</option>
<?php


$upper = intval(strftime('%Y'));
$lower = $upper - 150;




for($i = $upper; $i > $lower; $i--) {
?>
 <option value="<?php echo $i ?>" <?php if($_POST['dob_year'] == $i) echo 'selected="selected"' ?>><?php echo $i ?></option>

<?php
}

?>

</select>
<input type="submit" name="submit" value="submit" />
</form>

 

That will select the first select I manually created if no post value was found

Totally took a while to type this out, so im posting it anyways, but if your topic is solved, you can click the topic solved button at the bottom of the thread

 

 

There needs to be a little more clarification i'm afraid. Now under what circumstances is the user returning to the form. Are they clicking a link? page redirect (via header or javascript/html redirect) can you send any post data? Or is the user simply told to click the back button, or navigate the to the form on their own?

 

If you give the user a link or redirect some how, you can put some $_GET data right into the url. For example, a link back may look like

Oops! There was a problem with your form! Please click <a href="form.php?dobyear=1990">here</a> to view the form

 

obviously, for a redirect, just append the redirect url with whatever get data you want .Also note that in my example, I chose dobyear is my get data name, so I will have to refer to it in PHP as $_GET['dobyear']. If you  use a different name, like dob_year, or birthyear, or something, make sure you change your script accordingly

 

Now, in that case, we are basically sending some $_GET data through the url instead of through a form. Actually, when you use a form use's method is get, all it does is simply append the form data to the url in this fashion : page.php?form1=username&form2=password&submit=submit

 

Now, using your select script you can do something like

<select name="dob_year">
<?php


$upper = intval(strftime('%Y'));
$lower = $upper - 150;

for($i = $upper; $i > $lower; $i--) {
        //choose the selected
        //using the ternary operator
       $select = (isset($_GET['dobyear'])) ? $_GET['dobyear'] : NULL;
/*the ternary statement was equivalent to
if (isset($_GET['dobyear'])){
$select = $_GET['dobyear'];
}
else {
$select = NULL;
}
*/
?>
 <option value="<?php echo $i ?>" <?php if($select && $select == $i ) echo 'selected="selected"' ?>><?php echo $i ?></option>

<?php
}

?>

</select>

 

now if we want some default thing selected, thats pretty easy too, we can just add a simple if statement after the form loop

<select name="dob_year">
<?php


$upper = intval(strftime('%Y'));
$lower = $upper - 150;

for($i = $upper; $i > $lower; $i--) {
        //choose the selected
        //using the ternary operator
       $select = (isset($_GET['dobyear'])) ? $_GET['dobyear'] : NULL;

?>
 <option value="<?php echo $i ?>" <?php if($select && $select == $i ) echo 'selected="selected"' ?>><?php echo $i ?></option>

<?php
}
//if no get data set, then we want to make the default on selected, otherwise, just make it unselected
//again with ternary
echo ($select) ? '<option value="0" >Year</option>' : '<option value="0" selected="selected">Year</option>';

?>

</select>

 

now if you aren't, or can't send data, you can also use cookies. Cookies may be the better option, because it will work whether or not you send a link

 

now, to set cookies, use setcookie() (the link above) Remember, like other headers, you must set cookies before any script output.

 

Now the way we could use cookies here would be to set the cookies when the script starts, like

//lets save the birth year
$birth_cookie = $_POST['dob_year'];
//set the cookie
setcookie("dobyear", $birth_cookie);
//setcookie(name of cookie, value of cookie, date it expires)
//i left the last parameter out (date it expires) so it will expire when the
//user closes the page, or ends the current session somehow.
//now if we wanted to make it an hour from now, we use time()
// and add 1 hour (in seconds) to time, like so
setcookie("dobyear", $birth_cookie, time() + 60*60);

..do other stuff
if (form was a success){
//do mysql stuff

//now we can delete the cookies, because we dont need them any more
//to delete cookies, we basically set the death time to somewhere in the past
//there is some debate on the best way to do it, but using 1 as the expiration time
//seems to be the best. 1 represents the time 1 second after midnight on the unix epoch (which 
was during 1970)
setcookie("dobyear", '', 1);//i leave out the value, because it doesn't matter
//this is optional by the way. The cookie will expire on its own, depending on what
//you put the the date it expires parameter
}
else {
//there were errors
//do whatever error handling and stop
}

 

now, we can use the cookie like we used the get variable. But instead of using $_GET['dobyear'], replace all occurrences of it with $_COOKIE['dobyear']

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.