Jump to content

Code for a Changeable Birthday Date


timothyarden
 Share

Recommended Posts

Hi Everyone,

I am creating a signup form for a website. Within this form I have the following code:

<select id="date" name="birthdayday" value="">
						<option name="1" id="1" value="1">1</option>
						<option name="2" id="2" value="2">2</option>
						<option name="3" id="3" value="3">3</option>
						<option name="4" id="4" value="4">4</option>
						<option name="5" id="5" value="5">5</option>
						<option name="6" id="6" value="6">6</option>
						<option name="7" id="7" value="7">7</option>
						<option name="8" id="8" value="8">8</option>
						<option name="9" id="9" value="9">9</option>
						<option name="10" id="10" value="10">10</option>
						<option name="11" id="11" value="11">11</option>
						<option name="12" id="12" value="12">12</option>
						<option name="13" id="13" value="13">13</option>
						<option name="14" id="14" value="14">14</option>
						<option name="15" id="15" value="15">15</option>
						<option name="16" id="16" value="16">16</option>
						<option name="17" id="17" value="17">17</option>
						<option name="18" id="18" value="18">18</option>
						<option name="19" id="19" value="19">19</option>
						<option name="20" id="20" value="20">20</option>
						<option name="21" id="21" value="21">21</option>
						<option name="22" id="22" value="22">22</option>
						<option name="23" id="23" value="23">23</option>
						<option name="24" id="24" value="24">24</option>
						<option name="25" id="25" value="25">25</option>
						<option name="26" id="26" value="26">26</option>
						<option name="27" id="27" value="27">27</option>
						<option name="28" id="28" value="28">28</option>
						<option name="29" id="29" value="29">29</option>
						<option name="30" id="30" value="30">30</option>
						<option name="31" id="31" value="31">31</option>
					</select>
					<select name="birthdaymonth">
						<option id="January" name="January" value="January">January</option>
						<option id="Febuary" name="Febuary" value="Febuary">Febuary</option>
						<option id="March" name="March" value="March">March</option>
						<option id="April" name="April" value="April">April</option>
						<option id="May" name="May" value="May">May</option>
						<option id="June" name="June" value="June">June</option>
						<option id="July" name="July" value="July">July</option>
						<option id="August" name="August" value="August">August</option>
						<option id="September" name="September" value="September">September</option>
						<option id="October" name="October" value="October">October</option>
						<option id="November" name="November" value="November">November</option>
						<option id="December" name="December" value="December">December</option>
					</select>
					<select name="birthdayyear" selected="1996">
						<option name="1900" value="1900">1900</option>
						<option name="1901" value="1901">1901</option>
						<option name="1902" value="1902">1902</option>
						<option name="1903" value="1903">1903</option>
						<option name="1904" value="1904">1904</option>
						<option name="1905" value="1905">1905</option>
						<option name="1906" value="1906">1906</option>
						<option name="1907" value="1907">1907</option>
						<option name="1908" value="1908">1908</option>
						<option name="1909" value="1909">1909</option>
						<option name="1910" value="1910">1910</option>
						<option name="1911" value="1911">1911</option>
						<option name="1912" value="1912">1912</option>
						<option name="1913" value="1913">1913</option>
						<option name="1914" value="1914">1914</option>
						<option name="1915" value="1915">1915</option>
						<option name="1916" value="1916">1916</option>
						<option name="1917" value="1917">1917</option>
						<option name="1918" value="1918">1918</option>
						<option name="1919" value="1919">1919</option>
						<option name="1920" value="1920">1920</option>
						<option name="1921" value="1921">1921</option>
						<option name="1922" value="1922">1922</option>
						<option name="1923" value="1923">1923</option>
						<option name="1924" value="1924">1924</option>
						<option name="1925" value="1925">1925</option>
						<option name="1926" value="1926">1926</option>
						<option name="1927" value="1927">1927</option>
						<option name="1928" value="1928">1928</option>
						<option name="1929" value="1929">1929</option>
						<option name="1930" value="1930">1930</option>
						<option name="1931" value="1931">1931</option>
						<option name="1932" value="1932">1932</option>
						<option name="1933" value="1933">1933</option>
						<option name="1934" value="1934">1934</option>
						<option name="1935" value="1935">1935</option>
						<option name="1936" value="1936">1936</option>
						<option name="1937" value="1937">1937</option>
						<option name="1938" value="1938">1938</option>
						<option name="1939" value="1939">1939</option>
						<option name="1940" value="1940">1940</option>
						<option name="1941" value="1941">1941</option>
						<option name="1942" value="1942">1942</option>
						<option name="1943" value="1943">1943</option>
						<option name="1944" value="1944">1944</option>
						<option name="1945" value="1945">1945</option>
						<option name="1946" value="1946">1946</option>
						<option name="1947" value="1947">1947</option>
						<option name="1948" value="1948">1948</option>
						<option name="1949" value="1949">1949</option>
						<option name="1950" value="1950">1950</option>
						<option name="1951" value="1951">1951</option>
						<option name="1952" value="1952">1952</option>
						<option name="1953" value="1953">1953</option>
						<option name="1954" value="1954">1954</option>
						<option name="1955" value="1955">1955</option>
						<option name="1956" value="1956">1956</option>
						<option name="1957" value="1957">1957</option>
						<option name="1958" value="1958">1958</option>
						<option name="1959" value="1959">1959</option>
						<option name="1960" value="1960">1960</option>
						<option name="1961" value="1961">1961</option>
						<option name="1962" value="1962">1962</option>
						<option name="1963" value="1963">1963</option>
						<option name="1964" value="1964">1964</option>
						<option name="1965" value="1965">1965</option>
						<option name="1966" value="1966">1966</option>
						<option name="1967" value="1967">1967</option>
						<option name="1968" value="1968">1968</option>
						<option name="1969" value="1969">1969</option>
						<option name="1970" value="1970">1970</option>
						<option name="1971" value="1971">1971</option>
						<option name="1972" value="1972">1972</option>
						<option name="1973" value="1973">1973</option>
						<option name="1974" value="1974">1974</option>
						<option name="1975" value="1975">1975</option>
						<option name="1976" value="1976">1976</option>
						<option name="1977" value="1977">1977</option>
						<option name="1978" value="1978">1978</option>
						<option name="1979" value="1979">1979</option>
						<option name="1980" value="1980">1980</option>
						<option name="1981" value="1981">1981</option>
						<option name="1982" value="1982">1982</option>
						<option name="1983" value="1983">1983</option>
						<option name="1984" value="1984">1984</option>
						<option name="1985" value="1985">1985</option>
						<option name="1986" value="1986">1986</option>
						<option name="1987" value="1987">1987</option>
						<option name="1988" value="1988">1988</option>
						<option name="1989" value="1989">1989</option>
						<option name="1990" value="1990">1990</option>
						<option name="1991" value="1991">1991</option>
						<option name="1992" value="1992">1992</option>
						<option name="1993" value="1993">1993</option>
						<option name="1994" value="1994">1994</option>
						<option name="1995" value="1995">1995</option>
						<option name="1996" value="1996">1996</option>
						<option name="1997" value="1997">1997</option>
						<option name="1998" value="1998">1998</option>
						<option name="1999" value="1999">1999</option>
						<option name="2000" value="2000">2000</option>
						<option name="2001" value="2001">2001</option>
						<option name="2002" value="2002">2002</option>
						<option name="2003" value="2003">2003</option>
						<option name="2004" value="2004">2004</option>
						<option name="2005" value="2005">2005</option>
						<option name="2006" value="2006">2006</option>
						<option name="2007" value="2007">2007</option>
						<option name="2008" value="2008">2008</option>
						<option name="2009" value="2009">2009</option>
						<option name="2010" value="2010">2010</option>
						<option name="2011" value="2011">2011</option>
						<option name="2012" value="2012">2012</option>
					</select></p>

 

This code is then posted to a script called signup.php. In this script though, as you have probably noticed, I could submit my birthday as the 31st of February - Which is not a valid date.

 

For this I am hoping to have both a java script solution to change the days according to the month (example: facebook.com - signup page). I would also like to have a php option in case they have disabled java script. I am also open to solutions in other languages (though it might take a bit more explaining for me to understand).

 

Thanks for reading and also a big thanks to anyone who can help me out,

Timothy

Link to comment
Share on other sites

And while you're at it, you could drastically reduce the typing for those select fields using for() loops and arrays/foreach() loops.

 

<?php
// build day field
$curday = date('d');
echo "<select name=\"day\">\n";
for( $i = 1; $i < 32; $i++ ) {
   $sel = $curday == $i ? $sel = 'selected="selected"' : '';
   echo "<option value=\"$i\" $sel>$i</option>\n";
}
echo "</select>\n\n";

// build month field
$month = array( 1 => 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'); // display format is set when value is echoed
$curmonth = date('m');
echo "<select name=\"month\">\n";
foreach( $month as $k => $v ) {
   $sel = $k == $curmonth ? 'selected="selected"' : '';
   echo "<option value=\"$k\" $sel>" . ucfirst(substr($v, 0, 3)) . "</option>\n"; // Display format is set in this line using ucfirst and substr . . .
}
echo "</select>\n\n";

// build year field
$curyear = date('Y');
$yr_begin = $curyear - 100; // starts list from 100 years ago
$yr_end = $curyear + 0; // ends list at current year
echo "<select name=\"year\">\n";
for( $i = $yr_end; $i >= $yr_begin; $i-- ) {
   $sel = $i == $curyear ? 'selected="selected"' : '';
   echo "<option value=\"$i\" $sel>$i</option>\n";
}
echo "</select>\n\n";

 

About all you'd need to do is modify the fields to include the id= attribute, and change their name= attributes.

Link to comment
Share on other sites

Hi Pikachu2000 and Psycho,

 

Thank you for your responses.

Psycho: I do not understand how I would use the check date function to make this work more efficiently and change the day according to the month. If you are willing could you please explain and expand on what you mean? Thankyou :)

 

Pikachu 2000:

Would this code automatically update according to which month has been selected? Also could you explain a bit further into how it works and where about I would be putting/integrating this.

 

Thanks heaps,

Timothy

 

Link to comment
Share on other sites

You can call this function from your month menu, and year menu.

 

function generateBirthDayMenu($month,$year){
session_start();
$_SESSION['birthday_month']=$month;
$_SESSION['birthday_year']=$year;
$day_array=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28);
$month_array1=array("April", "June", "September", "November");
$month_array2=array("January", "March", "May", "July", "August", "October", "December");

if($month=="February" && ($year % 4) ==0 && (($year % 100) !=0 || ($year % 400) ==0)){
array_push($day_array, "29");
}
elseif(!isset($month)){
array_push($day_array, "29", "30", "31");
}
else{

}

foreach($month_array1 as $month_value){
if($month==$month_value){
array_push($day_array, "29", "30");
}
}

foreach($month_array2 as $month_value){
if($month==$month_value){
array_push($day_array, "29", "30", "31");
}
}

foreach($day_array as $day_item){
$day.='<option value="'.$day_item.'"'; if(isset($_SESSION['birthday_day']) && $_SESSION['birthday_day']==$day_item){ $day.=' selected="selected"'; } $day.='>'.$day_item.'</option>'."\n";
}

return $day;
}

 

You need to use AJAX, and send the values of the month and year selected with a onclick change in your year and month select menus.

 

For example the month drop down would need to have this added to the select tag:

onchange="sndReq(register_year.value,this.value);

 

The year menu would have to have this added to the select tag:

onchange="sndReq(this.value,register_month.value);

 

 

 

 

This is the page I use for my ajax call.

 

<?php
require_once 'function.php';
session_start();

if(!isset($month) || (isset($_GET['month']) && $_GET['month']!=$month)){
$month=$_GET['month'];
}

if(!isset($year) || (isset($_GET['year']) && $_GET['year']!=$year0)){
$year=$_GET['year'];
}

$form_day_menu=generateBirthDayMenu($month,$year);

echo "<select name=\"day\">
<option>Day</option>
$form_day_menu
</select>";
?>

 

You may or may not want to remove the sessions.

Link to comment
Share on other sites

Psycho: I do not understand how I would use the check date function to make this work more efficiently and change the day according to the month. If you are willing could you please explain and expand on what you mean? Thankyou :)

 

This is the PHP forum, thus I provided a PHP solution to do the server-side validation of the date. If you want the client-side solution for dynamically changing the date options, then post that request in the JavaScript forum.

Link to comment
Share on other sites

Thanks for the Post Pikachu 2000, I have not yet tested it yet - I will get back to you :).

 

Psycho, I did not mean could you give a java script solution, I was hoping you would be able to explain some more about the checkdate() function that you had given. Sorry if this is any inconvenience. If so please just ignore.

 

Timothy

Link to comment
Share on other sites

Thanks for the Post Pikachu 2000, I have not yet tested it yet - I will get back to you :).

 

Psycho, I did not mean could you give a java script solution, I was hoping you would be able to explain some more about the checkdate() function that you had given. Sorry if this is any inconvenience. If so please just ignore.

 

The checkdate() function is a built-in PHP function. I provided a link to the documentation for that function that explains what it does, what parameters it takes, the possible return values and even provides a couple examples of how to use it. I'm not sure I could do as complete a job of explaining it in this type of venue than the manual already provides. If there is something you don't understand about the documentation, then by all means ask that question and we will attempt to help. But, you have to be an active participant in the process.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.