SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Form question

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 => '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 "<p><label>Date of Death/label></p>";

    // Make the months pull-down menu:

    echo '<select name="month">';
    foreach ($months as $key => $value) {
    echo "<option value=\"$key\">$value</option>\n";
    }
    echo '</select>';

    // Make the days pull-down menu:

    echo '<select name="day">';
    foreach ($days as $value) {
    echo "<option value=\"$value\">$value</option>\n";
    }
    echo '</select>';

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

    Jim

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @jskintauy,

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

    Try this:

    PHP Code:

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

    // Months Array

     
    $months = array (=> '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>\n";
     }
     echo 
    '</select>';

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

     
    // Make the years pull-down menu:
     
    echo '<select name="year">';
     foreach (
    $years as $value)
     {
         echo 
    "<option value=\"$value\">$value</option>\n";
     }
     echo 
    '</select>'
    Last edited by John_Betong; Oct 1, 2012 at 23:02. Reason: spelling: not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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">

    <fieldset><legend>Your Information: </legend>

    <p><label>First Name: <br><input type="text" name="name" size="30" maxlength="40" /></label></p>

    <p><label>Last Name: <br><input type="text" name="name" size="30" maxlength="40" /></label></p>

    <p><label>Email Address: <br><input type="text" name="email" size="30" maxlength="60" /></label></p>

    <p><label>User Name: <br><input type="text" name="email" size="30" maxlength="60" /></label></p>

    <p><label>Password: <br><input type="text" name="email" size="30" maxlength="60" /></label></p>

    <p><label>Repeat Password: <br><input type="text" name="email" size="30" maxlength="60" /></label></p>

    </fieldset>

    <div id="labelseparator">

    <fieldset><legend>Deceased Persons Information</legend>

    <p><label>First Name: <br><input type="text" name="name" size="30" maxlength="40" /></label></p>

    <p><label>Last Name: <br><input type="text" name="name" size="30" maxlength="40" /></label></p>


    <?PHP

    // Months Array

    $months = array (1 => '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 "<p><label>Date of Death</label></p>";

    // Make the months pull-down menu:

    echo '<select name="month">';
    foreach ($months as $key => $value) {
    echo "<option value=\"$key\">$value</option>\n";
    }
    echo '</select>';

    // Make the days pull-down menu:

    echo '<select name="day">';
    foreach ($days as $value) {
    echo "<option value=\"$value\">$value</option>\n";
    }
    echo '</select>';

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

    </fieldset>

    </div>

    <p align="center"><input type="submit" name="submit" value="Submit My Information" /></p>

    </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.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What you have:

    PHP Code:
    $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>\n";
    }
    echo 
    '</select>';
    ?> 
    What you should do first (with some gotchas)

    PHP Code:
    // NB YEARS, plural, range arguments are comma separated
    $years range(19002012);  


    // ... 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?"

    PHP Code:

    $years 
    range(1900date("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.

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @Cups ;
    Pipped on the posting by two minutes
    This editor is hard work


    @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.

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

    Try this modified source now that I understand your requirements.
    PHP Code:

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

     
    $years = array('Years');
     
    $years array_merge($yearsrange(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>\n"
     } 
     echo 
    '</select>'

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

     
    // Make the years pull-down menu: 
     
    echo '<select name="year">'
     foreach (
    $years as $value
     { 
         echo 
    "<option value=\"$value\">$value</option>\n"
     } 
     echo 
    '</select>'
    Last edited by John_Betong; Oct 2, 2012 at 06:16. Reason: spelling. etc
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by jskintauy View Post
    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

    http://php.net/manual/en/ref.array.php
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •