Select option dropdownlist yii - php

When i select value from drop down list,
and I submit form, i want my drop down list to have selected value,
not to be on default value again.
I try like this, but not working:
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array('name'=>'countriesIssued'), $select = array($_POST['countriesIssued']));?>
Also i would like to add a first value to be default, not from db, i want to do it in the code like this, array('empty'=>'--Select country--')
but not working.
Thanks

If you must change the name of your dropdownList you must manually set the value of code to $_POST['countriesIssued'] in your controller/view. As for the default, prompt is used to set this.
<?php if(!$model->code) $model->code=$_POST['countriesIssued'];?>
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array(
'name'=>'countriesIssued','prompt'=>'--Select country--'
));?>

The second parameter (currently 'code') must be a key in this array: $countriesIssuedList
See for example here.
"Also i would like to add a first value to be default" ... perhaps you can use array_merge()?

Related

CakePHP 2.x: date input with empty option/values

I have a date input and I wish to have the first option empty or blank. That way the user will have to select something, if not, validation will pick it up.
By default today's date preselected on load. I have managed to input a blank value but I cannot get it as the selected option.
echo $this->Form->input('date_of_birth', array('empty' => ''));
You should use boolean on empty attribute:
echo $this->Form->input('date_of_birth', array('empty' => true));

How to make a readonly select dropdown?

I am trying following to make a dropdown readonly:
$element = $this->CreateElement('select', 'type');
$element->addMultiOptions( $Types );
$element->setRequired(true);
$element->setAttrib('readonly',true);
$element->setLabel('Type');
$elements[] = $element;
A dropdown is shown but I am able to select other values.
How to make a dropdown readonly ?
Thanks
You can use the read only attribute. You could avoid outputting a dropdown at all if it is not the best element to use in your particular case.
$element->setAttrib('disabled','disabled');
If you mean to only have one value selected, you could always disable it. Using your current framework, you might do something like:
$element->setAttrib('disabled', true);

PHP CodeIgniter - Dropdown using foreach does not show "selected" value

I am generating options for my dropdown using
// view.php
foreach ($plants as $row):
$options[$row->plant_id] = $row->plant_name;
endforeach;
and then lower in the HTML part of view.php
//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);
The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.
The HTML source also shows 'Select' in form_dropdown was ignored.
I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.
Any idea what is going on here or how to solve this issue?
You need to have the default option in your $options array...
So before your foreach, do:
$options = array('Select');
I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.
If you wanted to explicitly set this value as the default you would pass 0 as the default argument.

how to set an option for form->input( 'multiple'=>'checkbox')

i plan to set a checkbox with selected option in my form.
but i am unable to show my checkbox content in the form, i cant see any value instead of just a box for me to select.
how to show value while i using checkbox? i able to show my value while i using select.
this is in a HABTM model. any hints?
here is my selection code.
input('User',array('label'
=> 'Select Related Potential',
'multiple'=>'checkbox',
//'options' => $users,
'legend'=>$users,
//'value'=>$users,
//'id'=>$ownUserId,
'default'=>$ownUserId,
'style'=>'width:200px;height:100px',
'selected' => $ownUserId, )); ?>
This may be relevant:
You cannot use default to check a checkbox - instead you might set the value in $this->data in your controller, $form->data in your view, or set the input option checked to true.
For example:
// in Controller
$this->data['Model']['field'] = true;
Causes the field to have the value true, which will result in a checked checkbox for this field.
Or:
$form->input('Model.field', array('checked' => true));
Always checks the checkbox.
Or:
$form->input('Model.field', array(
'checked' => ($this->data['Model']['field'] == 'xxx')
));
Dynamically sets the checkbox based on whether $this->data['Model']['field'] is 'xxx' or not.
Sorry, completely misunderstood the question.
Did you find your users via list? The options array needs to be in a particular format, a normal find() won't do that.

Yii 1.1.3 setting selected value of dependent dropdown part 2

Previously I've created a question where I was wondering how to set selected value for dependent dropdown. That was easy, man adviced me just set second parameter.
echo CHtml::dropDownList('OpenLessons[Building]', $buildingClassID, $buildingList,array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('ajax/floorList'),
'update'=>'#OpenLessons_Floor',
)));
echo CHtml::dropDownList('OpenLessons[Floor]',$model->Class_ID, array(),array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('ajax/roomList'),
'update'=>'#OpenLessons_Class_ID',
)));
echo CHtml::dropDownList('OpenLessons[Class_ID]',$model->Class_ID, array());
where $buildingClassId is value of select. So that works fine for first select. But how can I set the same for dependent ones? When I look at it's code it looks like this:
<select name="OpenLessons[Floor]" id="OpenLessons_Floor">
</select>
I expected it to have some options, because I set default value for first one. So even if I put as second parameter of seconddropdown some value it wouln't be selected.
Any advices/suggestions?
UPDATE I'd mention that I don't see at XHR console call of floorlist. How can I make it?
UPDATE
$(document).ready(function()
{
$('#OpenLessons_Building').trigger('change');
$('#OpenLessons_Building option').trigger('click');
}).on('change','#OpenLessons_Building',function()
{
console.log('changed');
}).on('click','#OpenLessons_Building option',function()
{
console.log('clicked');
});
Tried like this. Both actions - change and click has happened because I can see output in console, but the dependent arrays are still empty.
You should use $("#OpenLessions_Building").on("change", function() { your code here });
The trigger function was actually does is execute the action (change or click in your case) of the dropdown as soon as the page loads.

Categories