Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I need to come up with an HTML page that provides 3 dropdowns for MonthName, Day, and Year. When user hits Submit, the date that was selected by the user is now formatted in this way: Month#/Day/Year along with the message "You have selected (date)."

The dropdown list has to be done using loops in PHP. I'm not sure how to get a selected "May" to come out as "5."

I don't have any code to work from because I'm not even sure where to begin. I'm not sure how to get HTML elements to interact with PHP.

Any ideas are helpful. I'm having a hard time figuring it out, let alone getting started.

How can I get the PHP dropdown list to be output in the formatted way?
Posted
Updated 21-Dec-13 12:46pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Dec-13 0:13am    
You need to start from the right side: learn how the Web works in principle. HTML does not "interact with PHP" at all. Just the whole idea is wrong. PHP just generates HTTP response in response to HTTP request, so it can generated any content of any type, including HTML.
You can use either a Web form or Ajax to send an HTTP request to server side...
—SA

1 solution

Hi Friend,

I understand the problems that beginners face. But I want to ask one, thing, do you have a logic ready in your mind. Logic is how the code is going to execute which doesn't imply any language boundary. This can be a simple flowchart or simple english like points approach. Here's an example of the latter.

1. First, the page will display three select boxes -> Day, Month, Year. This implies that I have to use some html to display this.

2. When a user will select the values and hit Submit button (implies, I have a form element in HTML), the data is going to some PHP script.
My PHP script is the same page that generated the HTML form (say, index.php).

3. This implies that I should have a method to detect if a user has not just loaded a page but rather has sent the form data to it.
This also implies that a branching is used. i.e. if-else block is used.
Like, If submitted == True, Then
// Do Something to process the form data and display the result
Else // Display the form to let the user input the data

4. Now, if user had submitted the data to me using the form, I have to get that data too and store it in a temporary variable also.
This depends on the method via which the form is submitted. For starters, I would say, there are two methods GET and POST, via which
a form data is submitted. When you see form data submitted inside a url like http://example.com/process.php?data=somedata&data2=something
This is the example of GET method. While in POST method, the data is posted in different way and you will not see any query string in
the url.

5. I should be using GET method at the moment, so I will have a method in PHP to get this data. this is $_GET['data']. Whatever is inside
the single quote is what the name attribute of the form elements like Textboxes, Selectboxes etc. What you get is the value posted to you.

6. Now that we have stored all the variables in the page, we will be processing the details ... Here's an example (Highly Commented in PHP)

PHP
    // First thing to check is if user has submitted any data to this page via the form
    // We will use if-else condition here
    // The isset function checks if the variable of the given name exists or not. The $_GET['submit'] is the variable
    // When you submit any form data via GET, the PHP's $_GET which is an associative array (More on this later), is 
    // used to store the data posted to page using query string. The name attribute of it is used as the key to the array
    // Here, a hidden field is given the name="submit". so, using the next line we will see if the user has clicked on submit button
    // or not. BTW, it can be easily fooled.
    
    if (isset($_GET['submit'])) {
        // Now if the user has submitted some data via the form generated below, it is going to be processed here
        // First step is storing the values submitted via the form. I will not consider any security issues at the moment
        $day = $_GET['day'];
        $month = $_GET['month'];
        $year = $_GET['year'];

        // Now we will process the received and stored data This is going to be dead simple
        // First we will generate a timestamp from the received date value
        $timestamp = strtotime($day.'/'.$month.'/'.$year);

        // Now we will display the time in a correct format
        $date = date('M/j/Y ', $timestamp);
        echo $date;

        // And a message to be displayed
        echo '<br /> You have entered the date : '. $date;
    }
    else {
        // Else, the user has not submitted any data, so we will be displaying the form to get input from user
        // To print something, we use echo method. It can work with both single quotes '' as well as double quotes "" there are
        // merits as well as demerits to both, you have to read books to know more. I will use single qoute here.
        echo '
              <form method="get" action="index.php">
                <select name="day">
             ';
        // We will use loop to display days 1-31 (it is a basic example so, we will not consider, at-least in the form part, about
        // 30/31 days or 28/29 days in a month
        // Simple for loop
        for ($i = 1; $i <= 31; $i++) { // $i is the variable. In PHP all variables are named like $var_name
            // We will generate option tag with name= value of i and inner text also is value of i
            echo '<option value="'. $i .'">'. $i .'</option>';
        }

        echo '</select> <br />
              <select name="month">
             ';
        // This time, we want to show name of the month, so we will store all the month names in an array.
        // The array below is known as enumerated array.
        $months = array (
                   1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec'
                  );

        // Now we will use a special type of loop especially for array iterations. This is a foreach loop
        foreach ($months as $key=>$value) {// I guess this line is self explanatory
           // What happens is that each element of an array is broken into two parts, first is it's key or index (here, enumerated index)
           // and second is the value at that index
           // Now we will generate option tags
           
           echo '<option name="'. $key .'">'. $value .'</option>';
        }

        echo '</select><br />
              <select name="year">
             ';
        // You can generate this range as per your need, I will generate -20 +20, i.e. -20 years back and +20 years to come.
        // Let us get the current year, this is done by two functions, first, is date() and second is time()
        $year = date('Y', time()); // The time() i.e. the second argument is actually optional. For format, you should refer to 
        // online PHP reference

        for ($i = $year - 20; $i <= $year + 20; $i++) {
           echo '<option name="'. $i .'">'. $i .'</option>';
        }

        echo '</select>
              <input type="hidden" name="submit" value="true" />
              <input type="submit" value="Submit" />
              </form>
             '; 
    }
?>


Hope that this simple example will help you solve the problem. Have a happy programming.

With Regards
Tushar Srivastava
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900