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>

Dani AI

Generated

The root cause is most likely the page structure rather than PHP syntax: the two <select> controls are in separate forms, so clicking a submit button only sends the fields inside that form. Combine both selects into a single form (or submit both values together via JavaScript/AJAX) and move request handling to the top of the script as suggested. As asked, decide whether you really need two lists/forms — usually one form with two selects is the simplest.

A small, safe server-side pattern (process before output, validate, then query) looks like this:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $start = filter_input(INPUT_POST, 'start_month', FILTER_VALIDATE_INT, ['options'=>['min_range'=>1,'max_range'=>12]]);
    $end   = filter_input(INPUT_POST, 'end_month',   FILTER_VALIDATE_INT, ['options'=>['min_range'=>1,'max_range'=>12]]);

    if ($start === false || $end === false) {
        $error = 'Please choose valid months.';
    } elseif ($start > $end) {
        $error = 'Start month must be earlier than or equal to end month.';
    } else {
        // Use a prepared statement (PDO) and bind :start/:end for the DB query
    }
}
?>

Practical notes: give option values numeric IDs (1..12) or ISO date strings (e.g. "2013-02-01") so comparisons and SQL BETWEEN become trivial; using month names forces extra parsing. For UX, follow and use client-side code to restrict the end-month list after the start is chosen — but never rely on client-side checks alone. Always validate/sanitize inputs server-side and use prepared statements to avoid SQL injection. For debugging, temporarily dump $_POST at the top to confirm what the browser actually sends.

Recommended Answers

All 4 Replies

Member Avatar for Member #949455

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 Member #120589

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.