Page 1 of 1

Php problem

Posted: Tue Jan 10, 2006 6:33 pm
by utahfriend
I am trying to create a drop down menu that a user can choose a month, day and year. Everything works find expect the year drop down menu does not work. I would appreciate any help in trying to find out why it doesn't work.

Here is the code

Code: Select all

<?PHP
                       define("ADAY", (60*60*24));
                        if (!checkdate($_POST['month'],$_POST['mday'],$_POST['year'])) {
	                        $nowArray = getdate();
					$mday = $nowArray['mday'];
	  				$mon = $nowArray['mon'];
					$year = $nowArray['year'];
   				} else {
					$mday = $_POST['mday'];
					$mon = $_POST['month'];
					$year = $_POST['year'];
   				}

				$start = mktime (12,0,0, $month,$day,$yr);
				$firstDayArray = getdate($start);

				echo "
				<form name=disp_events action=$_SERVER[PHP_SELF] method='post'>
     				<p align=\"center\">
			  
 			   <select name=\"day\">"; 
			 
				$days=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,29,30,31);
				for ($x=1; $x <= count($days); $x++) {
   					echo"<option value=\"$x\"";
  					 if ($x == $day) {
						echo " SELECTED";       
   					}
   					echo ">".$days[$x-1]."";	
				}
		       
		          echo "</select>

			    <select name=\"month\">";
			
				   $months=Array("January", "February", "March", "April", "May",
				   "June", "July", "August", "September", "October", "November", "December");
				   for ($x=1; $x <= count($months); $x++) {
   					echo"<option value=\"$x\"";
   					if ($x == $month) {
						echo " SELECTED";
   					}
   					echo ">".$months[$x-1]."";
				   }
				
				echo "</select>

				<select name=\"year\">";
				
					for ($x=2006; $x <= 2015; $x++) {
   						echo"<option";
  						if ($x == $year) {
							echo " SELECTED";
   						}
   						echo ">$x";
					}
			
				echo "</select>
				<input type=\"submit\" value=\"Go!\"></p>";
			
$month=sprintf("%02d",$month);
$day=sprintf("%02d",$day);

if ($month=="00") {
	$nowArray=getdate();
	$month=$nowArray['mon'];
	$yr=$nowArray['year'];
	$day=$nowArray['mday'];
	echo "no date<br>";
}
echo "Day: $day<br>";
echo "Month: $month<br>";
echo "Year: $year<br>";

$newdate="$year-$month-$day";
?>

Posted: Tue Jan 10, 2006 6:51 pm
by feyd
What does this have to do with databases?


You have mday in your expected post variables, but no such field in the form. You have both mon and month in expected post variables, but only month.

Year appears to be the only one that should work.


Moved to PHP - Code.

Posted: Tue Jan 10, 2006 7:05 pm
by utahfriend
Year, however is the only one that does not work. Day and month work just fine.

Posted: Tue Jan 10, 2006 8:18 pm
by feyd
how does it not work? It certainly appears that it should work.

Posted: Tue Jan 10, 2006 9:16 pm
by d3ad1ysp0rk

Code: Select all

for ($x=2006; $x <= 2015; $x++) {
                           echo"<option value=\"".$x."\"";
                          if ($x == $year) {
                            echo " SELECTED";
                           }
                           echo ">$x";
                    }
Try that.

Posted: Tue Jan 10, 2006 11:31 pm
by utahfriend
That still did not fix the problem. When you select the day and month, they change to the items selected. However, the year drop down menu always reverts back to the 2006 year, no matter what year you choose! I need to find out how to make it so that the year stays to the year selected.

Posted: Wed Jan 11, 2006 11:05 am
by utahfriend
I stripped down the code to point out my problem. If you run this code, it brings down a drop down menu for month and a drop down menu for year. When you select the month and click "Go!", the correct month shows below the menu in the $month field. However, when you select a year and click "Go!", the menu goes back to the first option (2006) and 2006 shows below in the $year field (even if 2010 was chosen). I need to find out why the year drop down menu does not retain the selection made and the correct year show in the $year field. Any help is greatly appreciated.

Code: Select all

<?PHP
define("ADAY", (60*60*24));
if (!checkdate($_POST['month'],$_POST['mday'],$_POST['year'])) {
	$nowArray = getdate();
	$mon = $nowArray['mon'];
	$year = $nowArray['year'];
   	} else {
	$mon = $_post['month'];
	$year = $_POST['year'];
}


echo "
<form name=disp_events action=$_SERVER[PHP_SELF] method='post'>
<p align=\"center\">
</select>
<select name=\"month\">";

$months=Array("January", "February", "March", "April", "May",
 	"June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++) {
	echo"<option value=\"$x\"";
	if ($x == $month) {
		echo " SELECTED";
	}
	echo ">".$months[$x-1]."";
}
				
							
echo "</select>
<select name=\"year\">";
for ($x=2006; $x <= 2015; $x++) { 
	echo"<option value=\"".$x."\""; 
	if ($x == $year) { 
		echo " SELECTED"; 
	} 
	echo ">$x"; 
} 

echo "</select>
<input type=\"submit\" value=\"Go!\"></p>";
			

echo "<p align=\"center\">Month: $month</p>";
echo "<p align=\"center\">Year: $year</p>";

?>

Posted: Wed Jan 11, 2006 12:02 pm
by sheila
What does checkdate() look like?

Posted: Wed Jan 11, 2006 12:42 pm
by RobertGonzalez
Try this and see what it does...

Code: Select all

<?php
define("ADAY", (60*60*24));
/* Changing this a little bit
if ( !checkdate($_POST['month'],$_POST['mday'],$_POST['year']) ) {
	$nowArray = getdate();
	$mday = $nowArray['mday'];
	$mon = $nowArray['mon'];
	$year = $nowArray['year'];
} else {
	$mday = $_POST['day']; // Changed mday to day to fit the form field data
	$mon = $_POST['month'];
	$year = $_POST['year'];
}
*/
$nowArray = getdate();
$mday = $nowArray['mday'];
$mon = $nowArray['mon'];
$year = $nowArray['year'];
if ( checkdate($_POST['month'], $_POST['day'], $_POST['year']) ) {
	$mday = $_POST['day'];
	$mon = $_POST['month'];
	$year = $_POST['year'];
}

$start = mktime (12,0,0, $mon,$mday,$year); // Changed $month to $mon, $day to $mday and $yr to $year
$firstDayArray = getdate($start);
?>

<form name="disp_events" action=<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p align="center">
<select name="day">
<?php
// Kill this array and just use a for loop from 1 to 31
//$days=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,29,30,31);
for ($x = 1; $x <= 31; $x++) {
	echo "<option value=\"$x\"";
	//if ($x == $day) {
	if ($mday == $x) {
		echo " selected";
	}
    echo ">$x</option>";
}
echo "</select>";

echo "<select name=\"month\">";
$months=Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++) {
	echo"<option value=\"$x\"";
	if ($mon == $x) {
		echo " selected";
	}
    echo ">" . $months[$x-1] . "</option>";
}
echo "</select>";

echo "<select name=\"year\">";
for ($x=2006; $x <= 2015; $x++) {
	echo"<option";
	if ($year == $x) {
		echo " selected";
	}
	echo ">$x</option>";
}
echo "</select>";
echo "<input type=\"submit\" value=\"Go!\"></p>";
            
$month=sprintf("%02d",$mon);
$day=sprintf("%02d",$mday);

/*
 * This is already done once at the top
 * Move this to top and change only if check_date returns true
if ($month=="00") {
	$nowArray=getdate();
	$month=$nowArray['mon'];
	$yr=$nowArray['year'];
	$day=$nowArray['mday'];
	echo "no date<br>";
}
*/
if ($month == '00') 
{
	echo "No date...<br />";
}
echo "Day: $day<br>";
echo "Month: $month<br>";
echo "Year: $year<br>";

$newdate="$year-$month-$day";
?>

Posted: Wed Jan 11, 2006 2:32 pm
by utahfriend
FANTASTIC!!! It works... Thank you so much!!!!!

Posted: Wed Jan 11, 2006 3:46 pm
by RobertGonzalez
You're welcome. You might want to clean up the code I posted a little bit by removing most of the comments.