Hello Everyone, I created a form so that users can select date range. Based on the date range later on it would show a graph. I am still in the first part trying to get the value selected by the user so I can generate the query from the database. How can I get those selected values?

See my code below:

<html>
    <head>
    <title>Months List</title>
    </head>
<?php
    $list = ARRAY('January','February','March','April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December');
    $list2 = ARRAY('January','February','March','April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December');
?>
<body bgcolor ="lightblue">
    <form id="form1" name="form1" method="post" action="">            
        <select id ="str" Name="Words">
            <option value="">--- Start Date ---</option>;         
            <?php                
                FOREACH($list AS $word){
                echo '<option value="'.$word.'">'.$word.'</option>';}
            ?> 
        </select>
        <input type="submit" name="Submit" value="Select" />
    </form>


    <form id="form2" name="form2" method="post" action="">            
        <select id="str2" Name="Words2">
            <option value="">--- End Date ---</option>;           
            <?php                
                FOREACH($list2 AS $word){
                echo '<option value="'.$word.'">'.$word.'</option>';}
            ?> 
        </select>
        <input type="submit" name="Submit" value="Select" />
    </form>
</body>
</html>
Member Avatar for LastMitch

@Samyx

I am still in the first part trying to get the value selected by the user so I can generate the query from the database. How can I get those selected values?

I thought at first you need a query SELECT for a database. Then I look closer and realized you just to SELECT a value from an array. Your arrays looks wrong.

You can read this (There's a few examples for you to used to get a value from the array plus show you how to fixed your arrays):

http://php.net/manual/en/language.types.array.php

Just pick an example you like and used it as query to select a value from an array.

Put the php code before the html, that will check whether either of the forms was submitted and what the selected value was:

<?php
// if either of submit buttons was pressed, $_POST['submit'] would be set
// and you can check the selected values
if(isset($_POST['submit'])) {

    // the selected value of the first select element (dropdown)
    $word1 = $_POST['Words'];

    // the selected value of the second select element (dropdown)
    $word2 = $_POST['Words2'];

    // do what you want with selected values
    ...
}
?>

You could also set different names to the submit buttons if you wanted to have more control.

Member Avatar for diafol

You may be better off placing numbers in the value of the option items as you could then do comparisons on them with DB queries. Storing text can be tricky:

 $list = array(1=>'January','February','March','April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December');
 $op='';
 foreach($list as $k=>v) $op .= "<option value='$k'>$v</option>";


   <select id ="str" Name="Words">
        <option value="">--- Start Date ---</option>;         
        <?php echo $op;?>
   </select>

   <select id ="str2" Name="Words2">
        <option value="">--- End Date ---</option>;         
        <?php echo $op;?>
   </select>

Why were you using two lists? $list and $list2 Were you going to do something with them?

Also - do you need 2 forms or are both fields meant to be submitted together?

See as for what i can see...you are doing it in a wrong way.
First of all if you want date range than latter date must be greater than start date.So for that you must do ajax call and depending on your selected value in start date,it must display end date that is equal or greater than start date.

If you want to use the value in same page than use javascript to get value:-

(document.getElementById('str').value)

Else if you want to pass value to next page then simply use

<?php
$_POST['str'];//if it is post method or else use $_GET['str'] depending on method type
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.