Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

cant set default option

In cakephp I cant set the selected option on a dropdown list  when i post.
Wjat happens is that I select an option and Post. Then the controller will find the data and go back to the same page but the selected value in the selected list is the the default.

So I should select a date from the list, press button and be redirected to the same page with the selected date. That date should be selected in the select list. The code works but the selected option doesnt stay the same when the page is redirected back to the same page from the controller.

  //controller
if (isset($this->request->data['button1'])) {
                $startdate=$this->request->data['Lesson']['ChooseAPaycycleStartDate'];
                 $enddate=date('Y-m-d',strtotime ( +13 . ' day' , strtotime ( $startdate)));
                 $this->redirect(array('action' => 'lesson_plan',$startdate,$enddate));
              }
/////view

  echo $this->Form->create();
            echo $this->Form->input('ChooseAPaycycleStartDate', array('options' => 
                    array('2014-10-06'=>'06-10-2014' , '2014-10-20'=>'20-10-2014', '2014-11-3'=>'3-11-2014', '2014-01-01'=>'01-01-2014')));
                    //   , 'type' => 'select', 'selected' => 2)));
 
            echo $this->Form->submit('Get Lesson Plans',array('name'=>'button1'));  
            echo $this->Form->end(); 
   

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

add

'selected' => '06-10-2014'

Open in new window


To your options array where the date value is the one selected
Avatar of jagguy

ASKER

Doesnt work as it will reset to the first date  everytime you post .This is tricky

  echo $this->Form->input('ChooseAPaycycleStartDate', array('options' => 
                    array('2014-10-06'=>'06-10-2014' , '2014-10-20'=>'20-10-2014', '2014-11-3'=>'3-11-2014', '2014-01-01'=>'01-01-2014','selected' => '20-10-2014')));
         

Open in new window

According to Cake documentation that is the way to do it. Have you tried going into the Cake source and seeing what is happening there?
Avatar of jagguy

ASKER

I have re read the docs over again.
I cant set the select to a variable

Here is a simple example and it doesnt set the selected value to the second option as it always defaults to the 0 option regardless of using strings or the word default.
 
  $avail=array(0 => 'Available', 1 => 'Not Available');
                      $val=1;
                    echo 'Monday :' . $this->Form->input('monavail', array( 'type' => 'select', 'options' => $avail ,'selectedt' => $val ));
ASKER CERTIFIED SOLUTION
Avatar of jagguy
jagguy
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jagguy

ASKER

i found the answer