I have a custom module in admin, where I am display values in dropdown format something like below using addField function.
<select onchange="checkSelectedBean(this.value)" class="required-entry select" name="nic_id" id="nic_id">
<option value="-1">Please Select</option>
<option value="1_1.1">America</option>
<option value="2_2.2">China</option>
<option value="3_3.3">India</option>
<option value="4_4.4">Japan</option>
</select>
Here 4 is the ID for Japan and 4.4 is its unique value.
Now, If I select Japan as an option and save it, I have split the post value i.e. 4_4.4 and the value saved in DB is 4 which is okay. But when I edit the same record its not showing me the saved option Japan but dropdown list of all options.
I need to know how can I make Japan as selected option in dropdown, when admin edits the same record.
Right now its showing me the dropdown list only and not the saved option.
I am fetching value from my Model/Nic.php like below.
public function toOptionArray()
 {
 $nicCollection = Mage::getModel('nic/nic')->getCollection();
 $nicarray = array(array('value'=>-1, 'label'=>'Please Select'));
 foreach ($nicCollection as $nicList){
 $nicarray[] = array('value'=>$nicList['nic_id']._.$nicList['nic_cost'],'label'=>$nicList['nic_name']);
 }
 return $nicarray;
 }
Admin form to fetch selected dropdown with values
$fieldset->addField('nic_id', 'select', array(
 'label' => Mage::helper('nic')->__('Nic'),
 'class' => 'required-entry',
 'values' => Mage::getModel('nic/nic')->toOptionArray(),
 'name' => 'nic_id',
 )); 
Pls Help.
Thanks.
- 
 is this option tags are hard coded or it is dynamic with php code ?Pradeep Sanku– Pradeep Sanku2014年06月10日 10:08:41 +00:00Commented Jun 10, 2014 at 10:08
 - 
 Please see my edited question. They are dynamic.Mark– Mark2014年06月10日 10:12:56 +00:00Commented Jun 10, 2014 at 10:12
 
1 Answer 1
<option value="<?php echo $nicarray['value']; ?>" <?php if($value_from_db == $nicarray['value']): echo "selected=selected"; else: ""; endif; ?> ><?php echo $nicarray['label']; ?></option>
Replace the array variable with actual array which you are using
here $value_from_db is the variable which is storing the selcted value.as you said some variable above giving value as 4.use that variable
- 
 I know the logic and how to make it selected. But I need to know how we do it in Magento.Mark– Mark2014年06月10日 10:34:06 +00:00Commented Jun 10, 2014 at 10:34
 - 
 see in the answer.if the db value and option array value are same when it is in the foreach loop then i had echo selected="selected". This will make the option as selected.Pradeep Sanku– Pradeep Sanku2014年06月10日 10:36:37 +00:00Commented Jun 10, 2014 at 10:36
 - 
 But how you do it in Magento Admin Form ?Mark– Mark2014年06月10日 10:44:29 +00:00Commented Jun 10, 2014 at 10:44
 - 
 ok can you post your magento form code please.i can help you.Pradeep Sanku– Pradeep Sanku2014年06月10日 10:49:30 +00:00Commented Jun 10, 2014 at 10:49
 - 
 Okay, pls see my edited question with
addFieldcodeMark– Mark2014年06月10日 10:52:56 +00:00Commented Jun 10, 2014 at 10:52