0

In select drop down, make one particular value selected from values that are coming in drop down list Cakephp3.0. i am using below code:

    $abc = 'india';
    echo $this->Form->input('location_id', 
['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false]);

Name of countries are coming in drop down list, but i want to make specific value selected which is set as variable $abc (i.e india).

1
  • Welcome to Stack Overflow :-) Please look at How to Ask and how to create a minimal reproducible example. This will help to get useful answers. Your question is difficult to read Commented Jul 6, 2016 at 12:00

4 Answers 4

1

Try this code :

use the 'default' key in

 $abc = array('1' => 'One','2'=> 'Two');
 echo  $this->Form->input('location_id',
                          'options' => $abc, 
                          default' =>$abc[1], 
                          ['empty' =>'Please select',
                          'required'=>'required', 
                          'class' => 'form-control',
                          'label'=>false]
                         ); 

where $abc[0] is the key of the item you want as selected

like this :

$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('field', $options, array('default' => 'F'));
Sign up to request clarification or add additional context in comments.

Comments

0

Use value option of form input helper. Like this :

$selected = 'india';
echo $this->Form->input('location_id', ['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false, 'value'=> $selected]);

Suggestion - Read the docs once and then start the development. You will never get stuck at such simple problems. And refer docs first whenever you get stuck at anything.

Comments

0
echo  $form->input('field_name', array(
            'type' => 'select',
    'options' => $arrayOfOptions, // typically set from $this->find('list') in controller 
    'label'=> 'Label name here',
    'value' => $arrProjectLeaderDetails['id'],  // specify default value 
    'escape' => false,  // prevent HTML being automatically escaped
    'error' => false,
    'class' => 'input_select_medium'
));

Comments

0

In cakephp4 :

echo $this->Form->select(
'field_name', [1,2,3,4,5,6],
['empty', => '(Click to select Something']
);

Scroll down a bit from here in the docs: https://book.cakephp.org/4/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.