HTML,PHP,JavaScript
プルダウンメニューで年月日を入力し,その値を受け取れるようにしたいです.
しかし,どうもうまくいかないのでダメなところを指摘して改善したいです。
条件として
(1) 存在する年月日のみが選択できること(=2月30日,4月31日などは選択できないこと)
(2) 閏年が考慮されていること(=2月が28日までのものと29までのものが存在)
(3) 現在までの年月日のみが選択できること(=未来は選択できないこと)
(4) 補足:送信ボタンを押した後に,自分自身に帰ってくる場合(action="index.php")に押下した値がリセットされずに残っているとなお良い
実際に自分で考えてみて,下のようになりました.
bodyタグを含め,その中身を貼ります.
PHP,JavaScript,CSSは同じファイルに記述するものとします.
条件分岐は(1)〜(3)を考慮しようと付けたものです.
イメージ的にはyear,month,dayに現在年月日を,
selectYear,selectMonth,selectDayに選択年月日を入れてプログラムを組んだつもりです.
HTML
1<body> 2<center> 3<body bgcolor onLoad="dateFunc()"> 4 <img src="LOGO.png" alt=""> 5 <h2>見出し</h2> 6 <?php 7 date_default_timezone_set('Asia/Tokyo'); 8 $year=filter_input(INPUT_GET,'year')?:date('Y'); 9 $month=filter_input(INPUT_GET,'month')?:date('n'); 10 $day=filter_input(INPUT_GET,'day')?:date('j'); 11 ?> 12 <form name="selectDate" method="get"> 13 <select name="selectYear"> 14 <?php 15 for($i=2015;$i<=date('Y');$i++){ 16 $selected=$i==$year?"selected":""; 17 print "<option value=\"{$i}\"{$selected}>{$i}</option>"; 18 } 19 ?> 20 </select> 年 21 <select name="selectMonth"> 22 </select> 月 23 <select name="selectDay"> 24 </select> 日 25 26 <script type="text/javascript"> 27 var year='<?php echo $year; ?>'; 28 var month='<?php echo $month; ?>'; 29 var day='<?php echo $day; ?>'; 30 function dateFunc(){ 31 var select1 = document.forms.selectDate.selectYear; 32 var select2 = document.forms.selectDate.selectMonth; 33 var select3 = document.forms.selectDate.selectDay; 34 35 select2.options.length=0; 36 select3.options.length=0; 37 38 if(select1.options[select1.selectedIndex].value==year){ 39 for(i=1;i<=month;i++){ 40 select2.options[i-1] = new Option(i); 41 } 42 } 43 else{ 44 for(i=1;i<=12;i++){ 45 select2.options[i-1] = new Option(i); 46 } 47 } 48 49 if(select1.options[select1.selectedIndex].value==year||select2.options[select2.selectedIndex].value==month){ 50 for(i=1;i<=day;i++) 51 select3.options[i-1] = new Option(i); 52 } 53 if(select2.options[select2.selectedIndex].value==4||select2.options[select2.selectedIndex].value==6||select2.options[select2.selectedIndex].value==9||select2.options[select2.selectedIndex].value==11){ 54 for(i=1;i<=30;i++) 55 select3.options[i-1] = new Option(i); 56 } 57 else if (select2.options[select2.selectedIndex].value==2){ 58 if((select1.options[select1.selectedIndex].value%4==0&&select1.options[select1.selectedIndex].value%100!=0)||select1.options[select1.selectedIndex].value%400==0){ 59 for(i=1;i<=29;i++) 60 select3.options[i-1] = new Option(i); 61 } 62 else{ 63 for(i=1;i<=28;i++) 64 select3.options[i-1] = new Option(i); 65 } 66 } 67 else{ 68 for(i=1;i<=31;i++) 69 select3.options[i-1] = new Option(i); 70 } 71 } 72</script> 73<input type="submit" value="送信"> 74</form> 75</center> 76</body> 77
回答2件
あなたの回答
tips
プレビュー