1

I'm writing the html codes in php file.
I have a dropdown to allow user query results with month & year that looks like this.

enter image description here

The query search on default current month and year, but for example when I search for April 2018, the results are be correct , but the dropdown selectoption still display July 2018.


My codes:

<table width="80%"  border="0" align="center" cellpadding="7" cellspacing="2">
<tr bgcolor="#ffffff">
  <td height="6" colspan="2" bgcolor="#E6E6FA">
    Month 
    <select name="soutlet" id="soutlet">
      <option selected="selected" value="7">July</option>
      <option value="">-----------</option>
      <option value="1">January</option>
      <option value="2">February</option>
      <option value="3">March</option>
      <option value="4">April</option>
      <option value="5">May</option>
      <option value="6">June</option>
      <option value="7">July</option>
      <option value="8">August</option>
      <option value="9">September</option>
       <option value="10">October</option>
      <option value="11">November</option>
      <option value="12">December</option>
    </select> 
    Year
    <select name="soutletto" id="soutletto">
      <option selected="selected" value="2018">2018</option>
      <option value="">-----</option>
      <option value="2016">2016</option>
      <option value="2017">2017</option>
      <option value="2018">2018</option>    
    </select>      
<input type="submit" name="btn_search" id="btn_search" value="Search" ></td>
</tr>

How do I modify my html codes in order the dropdown showing the correct month and year? If I search for April 2018 it will shows

enter image description here

5
  • 2
    You add selected="selected" to the option you want to be selected Commented Jul 23, 2018 at 2:13
  • @Mehdi by how ? Commented Jul 23, 2018 at 2:15
  • Just like you are currently doing to the July option, except you do it to the submitted one dynamically. Commented Jul 23, 2018 at 2:15
  • @Mehdi how do I put into the selected everytime user choose a value ? Commented Jul 23, 2018 at 2:17
  • Either use JS to select the option with a value or just create an if statement that checks for the submitted value Commented Jul 23, 2018 at 2:20

5 Answers 5

2

This is a little easier if you create your <select> using an array and a loop, ie.

<?php
$months = array(
                1=>"January",
                2=>"February",
                3=>"March",
                4=>"April",
                5=>"May",
                6=>"June",
                7=>"July",
                8=>"August",
                9=>"September",
                10=>"October",
                11=>"November",
                12=>"December"
);
?>
<select name="soutlet" id="soutlet">
  <option value="">-----------</option>
  <?php foreach($months as $key=>$month){
         //if posted value matches current value add selected
         $sel = ($key==$_POST['soutlet']) ? ' selected="selected"' : '';
         echo '<option value="'.$key.'" '.$sel.'>'.$month.'</option>';
       }
  ?>
</select> 
Sign up to request clarification or add additional context in comments.

3 Comments

You were faster, this is the recommended way IMO. Mind if you use the other foreach syntax and avoid the echo?
@Mehdi sure, you or the OP can use any syntax you want. I just used this as an example.
@Mehdi yeah, I just noticed I forgot to remove the OPs 'selected' line. I don't use the other syntax, so you are welcome to post an answer with that syntax and I would be happy to upvote it. Many ways to wok a dog.
0

you should add selected attribute when $_post['month'] = option value

$_post for post requests and $_get for get requests

Comments

0

Please create function for both year / month print and set function argument based on that

function month_select($month = '', $name = 'soutlet', $class = 'soutlet', $id = 'soutlet')
    {
        $month_sel = '<select id="'.$id.'" name="'.$name.'"  name="'.$class.'" >';
        $month_sel .= '<option value="">Select Month</option>';
        for($i = 1; $i <= 12; $i++)
        {
            $month_sel .= '<option value="'.$i.'" '.(($month == $i)?'selected="selected"':'').'>'.date("F", mktime(0, 0, 0, $i, 10)).'</option>';
        }
        $month_sel .= '</select>';
        return $month_sel;
    }
    function year_select($year = '', $name = 'soutletto', $class = 'soutletto', $id = 'soutletto')
    {
        $year_sel = '<select id="'.$id.'" name="'.$name.'"  name="'.$class.'" >';
        $year_sel .= '<option value="">Select Year</option>';
        for($i = 0; $i <= 5; $i++)
        {
            $cur_year = date("Y") - $i;
            $year_sel .= '<option value="'.$cur_year.'" '.(($cur_year == $year)?'selected="selected"':'').'>'.$cur_year.'</option>';
        }
        $year_sel .= '</select>';
        return $year_sel;
    }

    echo month_select($_GET['soutlet']);
    echo year_select($_GET['soutletto']);


OR 

You can use POST method


echo month_select($_POST['soutlet']);
echo year_select($_POST['soutletto']);

Comments

0

To solve this problem you can store month value into a variable while submitting the form. Like $month = $_POST['soutlet'] if you use POST method (do some validation ). Then you have to compare this value inside the html select > option and set it to be selected. Like this inside <option> tag

<option value="1" <?php echo isset($month) ? ($month == '1' ? 'selected' : '') : ''; ?> >January</option>

And don't forget to remove your statically defined selected from <option> use the same logic for the year.

4 Comments

Why storing in a session?
To make the value accessible until the session ends because he wants to show it by default selected otherwise we can use a normal variable to store the value. @Mehdi
That's not what he's asking for, he just wants to show the selected option when the form is submitted.
I am then changing my answer to the normal variable instead of using session. Thanks
0

Assuming you are posting to the same page, you can do something like this. (simplified for only 1 month and 1 year, but you get the idea)

table width="80%"  border="0" align="center" cellpadding="7" cellspacing="2">
<tr bgcolor="#ffffff">
<td height="6" colspan="2" bgcolor="#E6E6FA">
Month 
<select name="soutlet" id="soutlet"> 
<option <?php if($POST['soutlet']==7) echo 'select="selected"'; ?> value="7">July</option>
</select> 
Year
<select name="soutletto" id="soutletto">
  <option <?php if($POST[soutletto]==2018) echo 'select="selected"'; ?> value="2018">2018</option>
</select>      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.