chuflasky Posted January 6, 2012 Share Posted January 6, 2012 Hi, i just create a function to to create a drop drop down input field in which allow the user to select a particular date, but how do i retrieve the date chosen by the user. This is the code that i have to to create the input field in the form. //MAKE THE DATE PULL DONWN MENU// function event_date($event_month, $event_day, $event_year){ //make the month // $month = array(1 =>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'Octuber', 'November', 'December'); echo '<select name="$event_month">'; foreach($month as $key =>$value){ echo "<option value=\"$key\">$value</option>";} if($_POST["$event_month"] == "$event_month"){ echo 'selected = "selected" '; } echo '</select>'; // make the days echo '<select name="$event_day">'; for($day=1; $day<=31; $day++){ echo "<option value=\"$day\">$day</option>"; } if($_POST["$event_day"] == "$event_day"){ echo 'selected = "selected" '; }else if( $month[2] == "selected"){ for( $day=1; $day<=29; $day++){ echo "<option value=\"$day\">$day</option>"; }//end of for loop }// end of elseif echo '</select>';//make the year echo '<select name="$event_year">'; for($year = 2011; $year<=2020; $year++){ echo "<option value=\"$year\">$year</option>"; } if($_POST["$event_year"] == "$event_year"){ echo 'selected = "selected" '; } echo '</select>'; return TRUE; }//end of event_date function // MAKE THE EXPIRATION PULL-DOWN MENU// function expiration($expire_month, $expire_day, $expire_year){ $month = array(1 =>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'Octuber', 'November', 'December'); //make the month pulldown menu echo '<select name="$expire_month" id="expire">'; foreach($month as $key => $value){ echo "<option value=\"$key\">$value</option>"; } if($_POST["$expire_month"] == "$expire_month"){ echo 'selected ="selected" '; } echo '</select>'; echo '<select name="$expire_day" id="expire">'; for($day= 1; $day<=31; $day++){ echo "<option value=\"$day\">$day</option>\n"; } if($_POST["$expire_day"] == "$expire_day"){ echo 'selected = "selected" '; } echo '</select>';// MAKE THE YEAR PULL-DOWN echo '<select name="$expire_year" id="expire">'; for($year = 2011; $year<=2016; $year++){ echo "<option value=\"$year\" >$year</option>"; } if($_POST["$expire_year"] == "$year"){ echo 'selected = "selected" ' ; } echo '</select>'; return TRUE; }//end of expiration include("../include/footer.html"); and this code below retrieve the date chosen by the user which i think is not working. // // EVENT DATE // // require_once("post_events.php"); if(empty($_POST["$event_month"]) && empty($_POST["$event_day"]) && empty($_POST["$event_year"])){ $errors[] = '<p class="error"> - Please enter the event date.</p>'; }else{ $date_month = $_POST["$event_month"]; $date_day = $_POST["$event_day"]; $date_year = $_POST["$event_year"]; $date = $date_year . '-' . $date_month . '-'. $date_day ; } // expiration date // if( $_POST['place'] == 'onetime' && empty($_POST["$expire_month"]) && empty($_POST["$expire_day"]) && empty($_POST["$expire_year"]) ){ $errors[] = '<p class="error"> - Plese enter the expiration date.</p>'; }else{ $m = $_POST["$expire_month"]; $d = $_POST["$expire_day"]; $y = $_POST["$expire_year"]; $expiry_date = $y .'-'.$m.'-'.$d ; } Link to comment Share on other sites More sharing options...
Antonio Conte Posted January 6, 2012 Share Posted January 6, 2012 You retrieve it through the select name element. Skip using a variable there, and call it something like <select name="date">. You get it normally through post like $_POST['date']. This will retrieve the value. Link to comment Share on other sites More sharing options...
chuflasky Posted January 7, 2012 Author Share Posted January 7, 2012 Thanks Antonio, i will try it out and see if that work for me, i will let you know if i can get it to work. Link to comment Share on other sites More sharing options...
chuflasky Posted February 6, 2012 Author Share Posted February 6, 2012 OK, i have my function working now. The problem that i'm having now is that i can rip off the default value that in my case is the beginning of the date function that is "jan 1 12". I want the function to have a default value of a blank or 0 because right now the code always execute due to the default value. How can i alter my code in the function that it would print a blank at the option value. Can that be done or i would have to do it manually example <option>month</option><option value ='1'>jan</option>//and so on <option>day</option><option value='1'>1</option> //and so on <option>year</option><option value='12'>12</option>// an so on The following code is what i have now. //MAKE THE DATE PULL DONWN MENU// function event_date($event_month, $event_day, $event_year){ //make the month // $month = array(1 =>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'Octuber', 'November', 'December'); echo '<select name="event_month">'; foreach($month as $key =>$value){ echo "<option value=\"$key\">$value</option>"; } if($_POST["event_month"] == "$value"){ echo 'selected = "selected" '; } echo '</select>'; // make the days echo '<select name="event_day">'; for($day=1; $day<=31; $day++){ echo "<option value=\"$day\">$day</option>"; } if($_POST["event_day"] == "$day"){ echo 'selected = "selected" '; }else if( $month[2] == "selected"){ for( $day=1; $day<=29; $day++){ echo "<option value=\"$day\">$day</option>"; }//end of for loop }// end of elseif echo '</select>'; //make the year echo '<select name="event_year">'; for($year = 2011; $year<=2020; $year++){ echo "<option value=\"$year\">$year</option>"; } if($_POST["event_year"] == "$year"){ echo 'selected = "selected" '; } echo '</select>'; return TRUE; }//end of event_date function // MAKE THE EXPIRATION PULL-DOWN MENU// function expiration($expire_month, $expire_day, $expire_year){ $month = array(1 =>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'Octuber', 'November', 'December'); //make the month pulldown menu echo '<select name="expire_month" id="expire">'; foreach($month as $key => $value){ echo "<option value=\"$key\"> $value</option>"; } if($_POST["expire_month"] == "$value"){ echo 'selected ="selected" '; } echo '</select>'; echo '<select name="expire_day" id="expire">'; for($day= 1; $day<=31; $day++){ echo "<option value=\"$day\">$day</option>\n"; } if($_POST["expire_day"] == "$day"){ echo 'selected = "selected" '; } echo '</select>'; // MAKE THE YEAR PULL-DOWN echo '<select name="expire_year" id="expire">'; for($year = 2011; $year<=2016; $year++){ echo "<option value=\"$year\" >$year</option>"; } if($_POST["expire_year"] == "$year"){ echo 'selected = "selected" ' ; } echo '</select>'; return TRUE; }//end of expiration And the from where i call the function this what i have <p> <label for="date" class="label">Date:</label> <? event_date($_POST["event_month"], $_POST["event_day"], $_POST["event_year"]);?> </p> <p> <label for="expire_date" class="label">Expire Date:</label> <?php expiration( $_POST["expire_month"], $_POST["expire_day"], $_POST["expire_year"]); ?> </p> Thanks. 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted February 9, 2012 Share Posted February 9, 2012 Just Add an <option> before your for-loops and set them to be default. The value could be something like -1 as it's easy to check against. Link to comment Share on other sites More sharing options...
chuflasky Posted February 10, 2012 Author Share Posted February 10, 2012 Thanks Anthonio Conte, i did that and i gave the default value to be false but the code still execute, i was wondering if by any chance this have to be due to where the function is located. To be clear the function is in another file and i call the function where i keep the form validation code. Any thoughs. Link to comment Share on other sites More sharing options...
Antonio Conte Posted February 10, 2012 Share Posted February 10, 2012 I think it has to do with types. "false" as a string is not the same as the Boolean value. Try a negative number as suggested Link to comment Share on other sites More sharing options...
Recommended Posts