Solved

cant set default option

Posted on 2014-10-08
7
139 Views
Last Modified: 2014-11-01
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

0
Comment
Question by:jagguy
  • 4
  • 2
7 Comments
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 40370080
add

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

Open in new window


To your options array where the date value is the one selected
0
 

Author Comment

by:jagguy
ID: 40370511
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

0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 40370607
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?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jagguy
ID: 40407675
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 ));
0
 

Accepted Solution

by:
jagguy earned 0 total points
ID: 40407695
wait it works when I go out of the page and come back in. I had been refreshing the page which didnt  work with this.
0
 

Author Closing Comment

by:jagguy
ID: 40416975
i found the answer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Echo vs ?><?php  html code 4 44
converting numbers with php 3 26
Session timeout 5 13
Split long text string into shorter chunks 7 19
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now