drop-down-menucakephp-2.x

get the selected value from drop down menu in cakephp 2.x


I am trying to get the selected from a drop down menu. but it returns the index of the selected elements. I want to retrieve the value of that index. I want to get the value of $year. how can I get the value?

Controller that generate the years

<?php
class StockReportConditionsController extends AppController{
  public function report() {
    $years = array();
    for ($i = 0; $i < 4; $i++) {
      $year = date("Y");
      $year = $year -$i;
      $years[$i]=$year;

      }
      $this->set(compact('years'));
}

View

<h1>Weekly Report</h1>
<table>
    <tr>
        <th>Year</th>
        <th>Stores</th>
        <th>Department</th>
    </tr>
    <?php echo $this->Form->create('StockReport',array('url'=>array('type' => 'get','action'=>'search')));?>
<tr>
  <td>
    <?php echo $this->Form->input('year',array('type'=>'select','options'=>$years)); ?>
</td>

<td>
  <?php echo $this->Form->input('Store.name',array('type'=>'select','options'=>$stores)); ?>
</td>

<td>
  <?php echo $this->Form->input('StockReportCondition.bu_no',array('type'=>'select','options'=>$departments)); ?>
</td>
</tr>
<?php echo $this->Form->end(__('Search'));?>
</table>

Controller

<?php
class StockReportsController extends AppController{
  public function search(){
    $year = $this->request->data['StockReport']['year'];
    echo $year;
    $store_no = $this->request->data['Store']['name'];
    echo $store_no;
    $dept_no = $this->request->data['StockReportCondition']['bu_no'];
    echo $dept_no;
    $result = $this->StockReport->find('all',array(
        'conditions'=>array('yearweek'=>'', 'bu_no'=>'','tn_no'=>'')
    ));
  }
}

Solution

  • It would seem that there's probably never a useful thing that you could do with the zero-based index of the year, and you'd only ever want the actual year value. If that's the case, then set up your options array so that the index and value are the same:

    $years[$year]=$year;