user9895458
user9895458

Reputation:

PHP - How do I show the correct selected <option> value?

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

Upvotes: 1

Views: 75

Answers (5)

Marios
Marios

Reputation: 181

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>      

Upvotes: 0

MNI Noyon
MNI Noyon

Reputation: 98

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.

Upvotes: 0

Sean
Sean

Reputation: 12433

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> 

Upvotes: 2

Jaydp
Jaydp

Reputation: 1039

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']);

Upvotes: 0

Abdi
Abdi

Reputation: 608

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

$_post for post requests and $_get for get requests

Upvotes: 0

Related Questions