Form question

Hi All,
This seems simple, but I am not having much luck.

I want to have a form that displays date fields (in a form where the label is “Date of Birth:”) that shows “Day, Month, Year” in the dropdowns but also the possible values.

Here is the code.

<?PHP

    // Months Array

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

    // Day and Year Arrays
    $day = array(1Day', range(1-31));
    $year = array('Year' range(1900-2012))

    // Label for date of death
    echo "&lt;p&gt;&lt;label&gt;Date of Death:&lt;/label&gt;&lt;/p&gt;";

    // Make the months pull-down menu:

    echo '&lt;select name="month"&gt;';
    foreach ($months as $key =&gt; $value) {
        echo "&lt;option value=\\"$key\\"&gt;$value&lt;/option&gt;\

";
}
echo ‘</select>’;

// Make the days pull-down menu:

    echo '&lt;select name="day"&gt;';
    foreach ($days as $value) {
        echo "&lt;option value=\\"$value\\"&gt;$value&lt;/option&gt;\

";
}
echo ‘</select>’;

    // Make the years pull-down menu:
    echo '&lt;select name="year"&gt;';
    foreach ($years as $value) {
        echo "&lt;option value=\\"$value\\"&gt;$value&lt;/option&gt;\

";
}
echo ‘</select>’;
?>

Of course, Month displays as needed, but the others do not work and I have tried treating this as a multi-dimensional array to no avail. Any thoughts, because I am stuck on something simple (again :slight_smile: )

Jim

@jskintauy,

Please try to:

  1. use PHP tags to enclose your code
  2. set error_reporting
  3. set display_errors

Try this:



error_reporting(-1);
ini_set('display_errors',true);

// Months Array

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

 // Day and Year Arrays
 #$day = array('Day', range(1-31));
 #$year = array('Year' range(1900-2012));

 $days = range(1,31);
 $years = range(1900,2012);
 #echo '<pre>';
    # print_r($days);
 #echo '</pre>';

// Label for date of birth
 echo "<label>Date of Birth: &nbsp; </label>";

 // Make the months pull-down menu:
 echo '<select name="month">';
 foreach ($months as $key => $value)
{
    echo "<option value=\\"$key\\">$value</option>\
";
 }
 echo '</select>';

 // Make the days pull-down menu:
 echo '<select name="day">';
 foreach ($days as $value)
 {
     echo "<option value=\\"$value\\">$value</option>\
";
 }
 echo '</select>';

 // Make the years pull-down menu:
 echo '<select name="year">';
 foreach ($years as $value)
 {
     echo "<option value=\\"$value\\">$value</option>\
";
 }
 echo '</select>';					


Hi John,

All the stuff with the tags and error reporting is already there.

If you look at the original code the month displays the way that I want. It shows month, followed by the values. The array includes the word ‘Month’ and does not include a range. If I rewrote the arrays to include the words ‘Day’ and ‘Year’ and all the values in the range, it would work fine.
Following is the code that “works” but it is ugly as the arrays don’t have ranges and all the values are spelled out.

<div>
<h2>Register</h2>

<form action=“pursue_form.php” method=“post”>

&lt;fieldset&gt;&lt;legend&gt;Your Information: &lt;/legend&gt;

&lt;p&gt;&lt;label&gt;First Name: &lt;br&gt;&lt;input type="text" name="name" size="30" maxlength="40" /&gt;&lt;/label&gt;&lt;/p&gt;

&lt;p&gt;&lt;label&gt;Last Name: &lt;br&gt;&lt;input type="text" name="name" size="30" maxlength="40" /&gt;&lt;/label&gt;&lt;/p&gt;

&lt;p&gt;&lt;label&gt;Email Address: &lt;br&gt;&lt;input type="text" name="email" size="30" maxlength="60" /&gt;&lt;/label&gt;&lt;/p&gt;

&lt;p&gt;&lt;label&gt;User Name: &lt;br&gt;&lt;input type="text" name="email" size="30" maxlength="60" /&gt;&lt;/label&gt;&lt;/p&gt;

&lt;p&gt;&lt;label&gt;Password: &lt;br&gt;&lt;input type="text" name="email" size="30" maxlength="60" /&gt;&lt;/label&gt;&lt;/p&gt;

&lt;p&gt;&lt;label&gt;Repeat Password: &lt;br&gt;&lt;input type="text" name="email" size="30" maxlength="60" /&gt;&lt;/label&gt;&lt;/p&gt;

&lt;/fieldset&gt;

&lt;div id="labelseparator"&gt;

&lt;fieldset&gt;&lt;legend&gt;Deceased Persons Information&lt;/legend&gt;

    &lt;p&gt;&lt;label&gt;First Name: &lt;br&gt;&lt;input type="text" name="name" size="30" maxlength="40" /&gt;&lt;/label&gt;&lt;/p&gt;

    &lt;p&gt;&lt;label&gt;Last Name: &lt;br&gt;&lt;input type="text" name="name" size="30" maxlength="40" /&gt;&lt;/label&gt;&lt;/p&gt;


    &lt;?PHP

    // Months Array

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

    // Day and Year Arrays

   $days = array('Day  ', 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);
    $years = array('Year', 1900, 1901, 1902, 1903, 1904, 1905, 1907, 1908, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972,
        1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020);

    // Label for date of death
    echo "&lt;p&gt;&lt;label&gt;Date of Death&lt;/label&gt;&lt;/p&gt;";

// Make the months pull-down menu:

    echo '&lt;select name="month"&gt;';
    foreach ($months as $key =&gt; $value) {
        echo "&lt;option value=\\"$key\\"&gt;$value&lt;/option&gt;\

";
}
echo ‘</select>’;

// Make the days pull-down menu:

    echo '&lt;select name="day"&gt;';
    foreach ($days as $value) {
        echo "&lt;option value=\\"$value\\"&gt;$value&lt;/option&gt;\

";
}
echo ‘</select>’;

// Make the years pull-down menu:
echo ‘<select name=“year”>’;
foreach ($years as $value) {
echo “<option value=\”$value\">$value</option>
";
}
echo ‘</select>’;
?>

&lt;/fieldset&gt;

&lt;/div&gt;

&lt;p align="center"&gt;&lt;input type="submit" name="submit" value="Submit My Information" /&gt;&lt;/p&gt;

</form>
</div>

Of course, I would greatly prefer to use the range as part of the array instead of having to spell out all the numbers / years. This seems simple enough, but I am struggling.

BTW - this is not yet styled much and obviously has no validation. That is the next step after resolving the issue of how it displays.

What you have:


$year = array('Year' range(1900-2012));


... later

// Make the years pull-down menu:
echo '<select name="year">';
foreach ($years as $value) {
echo "<option value=\\"$value\\">$value</option>\
";
}
echo '</select>';
?>

What you should do first (with some gotchas)


// NB YEARS, plural, range arguments are comma separated
$years = range(1900, 2012);


// ... later

// add the html later, kiss
// use OS independent constant PHP_EOL to add line ends


// prefer this variable names structure for array
// loops : foreach( plural as singlular ) ...
// you will get less mixed up

foreach ($years as $year) {
echo $year . PHP_EOL ;
}

?>

Also when done, unless you have pressing needs, initialise your variable as physically close to where you end up using it. It’ll save you scrolling up and down code and wondering to yourself, “wft did that come from?”



$years = range(1900, date("Y");
  foreach ($years as $year) {
    echo $year . PHP_EOL ;
  }

?>

ps added date(“Y”) so you can be lazy, and not have to hand code this file every year. :wink:

@Cups;
Pipped on the posting by two minutes :slight_smile:
This editor is hard work :frowning:

@jjskintauy;

Please, enclose you php script in PHP tags enclosed by square brackets and omit the spaces inside the square brackets.

[ php ]
your code
[ /php ]

Your original script had errors which I found confusing.


  $day = array(1Day', range(1-31));

Try this modified source now that I understand your requirements.



 $days  = array('Days');
 $days  = array_merge($days, range(1,31) );

 $years = array('Years');
 $years = array_merge($years, range(1900,2012));

 #echo '<pre>';
    # print_r($days);
 #echo '</pre>';

// Label for date of birth
 echo "<label>Date of Birth: &nbsp; </label>";

 // Make the months pull-down menu:
 echo '<select name="month">';
 foreach ($months as $key => $value)
{
    echo "<option value=\\"$key\\">$value</option>\
";
 }
 echo '</select>';

 // Make the days pull-down menu:
 echo '<select name="day">';
 foreach ($days as $value)
 {
     echo "<option value=\\"$value\\">$value</option>\
";
 }
 echo '</select>';

 // Make the years pull-down menu:
 echo '<select name="year">';
 foreach ($years as $value)
 {
     echo "<option value=\\"$value\\">$value</option>\
";
 }
 echo '</select>'; 					


Hi John,

Thanks so much. I should have been clearer in my original post. My bad.
I was unaware of array_merge. This was obviously exactly what I needed.

Jim

'glad I was able to help.

Take a look at the online PHP manual concerning the array functions. Complete with examples and many I have never used :slight_smile:

http://php.net/manual/en/ref.array.php

Hi Cups,

Thanks for the excellent reply and the best practices. I was aware of the date function so that was coming. Everything else is just gold for me. I really appreciate your comment concerning the PHP_EOL, I didn’t understand the difference and decided to get more information. Makes sense.

Jim