Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi,

there is an exercise from page 286, chapter 10, "make the function in menus.php take arguments to indicate the starting year and the number of years to generate. [...] rewrite the function body so that it uses these values in the year for loop". So, we may have two fields for entering the starting year (for ex. 1990) and the numbers of years (for ex. 15), in order to create a pull-down menu by a user-defined function (1990, 1991, 1992 etc).

The thing is, how on earth can i retrieve the chosen year from the select-list, in order to print a message like "you have chosen year X"?

The code as far as i was able to write it, is:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="content-type" content="text/xml; charset=utf-8" />
  <title>Menu</title>
 </head>
 <body>
 <?php
 //Define function that makes pull-down menus for the years:
 function make_date_menus ($start_year, $years) {
   print '<select name="year_list">';
   for($y=$start_year; $y<=($start_year+$years); $y++) {
     print "\n<option value=\"$y\">$y</option>";
   }
   print "</select>\n";
 } // End of make_date_menus

 //Check for a form submission:
 if ($_SERVER['REQUEST_METHOD']=='POST') {
   if (is_numeric($_POST['start_year']) && is_numeric($_POST['years'])) {
     if ($_POST['start_year']>0 && $_POST['years']>0) {
     make_date_menus ($_POST['start_year'], $_POST['years']);
   } else { //Negative numbers entered
     print "<p><em> Please enter positive numbers!</em></p>";
   }  
   } else { //Incorrect values entered
     print '<p><em> Please enter a valid starting year and number of years!</em></p>';
   }
 }
 ?>

   <form action="menus.php" method="post">
     <p>Enter the starting year: <input type="text" name="start_year" /></p>
     <p>Enter the number of years to generate: <input type="text" name="years" /></p>
     <input type="submit" name="submit" value="Make the menu!" />
   </form>  
 </body>    
</html>

Thanks!

Link to post
Share on other sites

Hi,

there is an exercise from page 286, chapter 10, "make the function in menus.php take arguments to indicate the starting year and the number of years to generate. [...] rewrite the function body so that it uses these values in the year for loop". So, we may have two fields for entering the starting year (for ex. 1990) and the numbers of years (for ex. 15), in order to create a pull-down menu by a user-defined function (1990, 1991, 1992 etc).

The thing is, how on earth can i retrieve the chosen year from the select-list, in order to print a message like "you have chosen year X"?

The code as far as i was able to write it, is:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="content-type" content="text/xml; charset=utf-8" />
  <title>Menu</title>
 </head>
 <body>
 <?php
 //Define function that makes pull-down menus for the years:
 function make_date_menus ($start_year, $years) {
   print '<select name="year_list">';
   for($y=$start_year; $y<=($start_year+$years); $y++) {
     print "\n<option value=\"$y\">$y</option>";
   }
   print "</select>\n";
 } // End of make_date_menus

 //Check for a form submission:
 if ($_SERVER['REQUEST_METHOD']=='POST') {
   if (is_numeric($_POST['start_year']) && is_numeric($_POST['years'])) {
     if ($_POST['start_year']>0 && $_POST['years']>0) {
     make_date_menus ($_POST['start_year'], $_POST['years']);
   } else { //Negative numbers entered
     print "<p><em> Please enter positive numbers!</em></p>";
   }  
   } else { //Incorrect values entered
     print '<p><em> Please enter a valid starting year and number of years!</em></p>';
   }
 }
 ?>

   <form action="menus.php" method="post">
     <p>Enter the starting year: <input type="text" name="start_year" /></p>
     <p>Enter the number of years to generate: <input type="text" name="years" /></p>
     <input type="submit" name="submit" value="Make the menu!" />
   </form>  
 </body>    
</html>

Thanks!

 

 

 

 

 

Check my code out:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Date Menus</title>

</head>

<body>

<?php // Script 10.1 - menus.php

/* This script defines and calls a function. */

 

// This function makes three pull-down menus for the months, days, and years.

function make_date_menus($start_year, $end_year = 2012) {

 

// Array to store the months:

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

 

// Make the month pull-down menu:

print '<select name="month">';

foreach ($months as $key => $value) {

print "\n<option value=\"$key\">$value</option>";

}

print '</select>';

 

// Make the day pull-down menu:

print '<select name="day">';

for ($day = 1; $day <= 31; $day++) {

print "\n<option value=\"$day\">$day</option>";

}

print '</select>';

 

// Make the year pull-down menu:

print '<select name="year">';

//$start_year = date('Y');

for ($y = $start_year; $y <= $end_year; $y++) {

print "\n<option value=\"$y\">$y</option>";

}

print '</select>';

 

} // End of make_date_menus() function.

 

// Make the form:

print '<form action="" method="post">';

make_date_menus(2011, 2050);

 

print '</form>';

 

?>

</body>

</html>

 

OK,this is not a so user-friendly...but that's cool,I think! :P

Link to post
Share on other sites

First, excellent work so far. Second, to select the chosen year, you'll need to make the select menu part of a separate form that gets submitted to another page. That's the easy way, at least.

 

Thanks Larry for your suggestion!

I made the select menu part of an another form that retrieves the chosen year and prints it to the same page, using the $_GET method. The affected part of the script is:

 

 if ($_SERVER['REQUEST_METHOD']=='POST') {
   if (is_numeric($_POST['start_year']) && is_numeric($_POST['years'])) {
     if ($_POST['start_year']>0 && $_POST['years']>0) {
     print '<form action="menus.php" method="get">'; //MAKE THE OTHER FORM FOR THE SELECT-MENU!
     make_date_menus ($_POST['start_year'], $_POST['years']);
     print '<input type="submit" name="submit" value="Select the Year" /></form>';
   } else { //Negative numbers entered
     print "<p><em> Please enter positive numbers!</em></p>";
   }  
   } else { //Incorrect values entered
     print '<p><em> Please enter a valid starting year and number of years!</em></p>';
   }
 } elseif ($_SERVER['REQUEST_METHOD']=='GET' && is_numeric($_GET['year_list'])) { //PRINTS THE SELECTED YEAR! 
    print '<p>Selected year is: '.$_GET['year_list'].'</p>';
 }

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...