I have a select on a form that updates the backend db ok. My problem is that when the form is displayed, it always shows the top record in the db and not the selected.
I've googled and read answers on stackoverflow among others but everything I try either doesn't work or breaks functionality.
$registrants points to a select on a registrants table.
$affiliates points to a select on an affiliates table
Here is the select:
<select name="affiliate_id" class="form-control">
<?php foreach($affiliates as $display => $value) { ?>
<option value='<?= $value['affiliate_id'] ?>' <?php if($registrants['affiliate_id'] == $value['affiliate_id']) { ?>selected='selected'<?php } ?>><?= $value['affiliate_name'] ?>
</option>
<?php } ?>
</select>
Thanks for looking it over.
You could clean up your code a bit and create a variable $selected, which either is empty or a string 'selected':
<select name="affiliate_id" class="form-control">
<?php
foreach($affiliates as $value):
$selected=$registrants['affiliate_id'] == $value['affiliate_id']?'selected':'';
?>
<option value="<?=$value['affiliate_id']?>" <?=$selected?> >
<?= $value['affiliate_name'] ?>
</option>
<?php
endforeach;
?>
</select>
or you could use the CI function form_dropdown(), for that don't forget to load the form helper. This method simplifies a bit the use of drop down lists:
$this->load->helper('form');
$data=array('class'=>'$affiliates');
// get the selected item
<?php
foreach($affiliates as $value){
$selected=$registrants['affiliate_id'] == $value['affiliate_id']?$value['affiliate_id']:'';
if($selected){break;}
}
?>
<?=form_dropdown('affiliate_id', $affiliates, $selected, $data);?>
I made a Joomla module which contains a form element select box. On selecting, it submits the form and the value of selected item is stored in a session variable.
<?php
defined('_JEXEC') or die;
$session = JFactory::getSession();
if (isset($_POST["country"])){
$session->set('cont', $_POST["country"]);
}
?>
<div id="dropdown">
<form action="" method="post" name="country"><label for="country"></label>
<select id="country" name="country" onchange="this.form.submit()">
<?php
$rows = Array('UAE', 'KSA', 'OMAN');
foreach($rows as $row){
if($row == $session->get('cont')){
$isSelected = ' selected="selected"';
}
else {
$isSelected = '';
}
echo "<option ".$isSelected.">".$row."</option>";
}
?>
</select>
</form>
</div>
and in K2 item page the code is
<?php
if($session->get('cont') == "KSA"){
echo "SR-".number_format($this->item->ksaprice);
}elseif($session->get('cont') == "OMAN"){
echo "OMR-".number_format($this->item->omanprice);
}else
echo "AED-".number_format($this->item->uaeprice);
?>
Now the problem is when I select a country, page reloads, the session is created successfully but the price doesn't change as the page needs to be refreshed again to get the session's value.
I need to change the price without refreshing the page again.
i have a form that uses a drop down and im using codeigniter , form helper and form validation
so when i get a validation error in my form , all the correctly entered fields are populated using set_value of codeigniter , however this doesnt work for dropdown
im doing :
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
when get error in form , always the first value of dropdown is selected and not previously set
Any ideas , what am i doing wrong?
The correct method to use set_select() is
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
So Andras Ratz method/answer won't work.
Your method is correct.
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
However remember that set_value() is only going to return the value if you have setup a validation rule for it, in your case for mid. You could try as below -
<?php echo form_dropdown('mid', $id_map, (isset($_POST['mid']) ? $_POST['mid'] : ''), 'id="mid"') ?>
or just set a validation rule for mid too
It's because you have to use the set_select() function , not the set_value();
You can check that here:
http://codeigniter.com/user_guide/helpers/form_helper.html
And the right syntax:
EDIT :
<?php echo form_dropdown('mid', $id_map, set_select('mid'), 'id="mid"'); ?>
If you have a default value, that should go in the 2. parameter for set_select.
I hope this will work.
<?php // Change the values in this array to populate your dropdown as required
$options = array(
'active' => 'Active',
'inactive' => 'Inactive',
);
echo form_dropdown('mid', $options,$id_map, ' id="mid"');
?>
What about if the the form select content is selected from a foreach loop like this
<?php foreach ($roles as $role) {
echo "<option value=" . $role->id . ">" . $role->role_name . "</option>";
}
?>
If you do not have a validation rule on the actual dropdown field, then
echo form_dropdown('mid',$id_map);
is sufficient.
I can get countries like this:
$countryCollection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
And they are listed somewhere in the object, but, how do I toOptionsArray them out?
I'm after only the options per website rather than complete listi.e. I want Angola, Antarctica and so on out of the list. (Sorry Angolans and penguins.)
Its actually the obvious answer:
$countryList = Mage::getModel('directory/country')->getResourceCollection()
->loadByStore()
->toOptionArray(true);
also check out http://fishpig.co.uk/magento-tutorials/list-countries-for-drop-down-in-magento for more info on creating drop-down lists and such with the country list.
<?php
$_countries = Mage::getResourceModel('directory/country_collection')
->loadData()
->toOptionArray(false);
$allowed = Mage::getStoreConfig('general/country/allow');
if (count($_countries) > 0) { ?>
<div class="input-box">
<select name="country" id="country" class="validate-select" title="Country" >
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country){
if(!in_array($_country['value'],explode(',',$allowed))){
continue;
} ?>
<option value="<?php echo $_country['value']; ?>" <?php echo $formData['country'] == $_country['value'] ? ' selected="selected"' : '';?>>
<?php echo $_country['label'] ?>
</option>
<?php
} ?>
</select>
<?php } ?>
how to get the timezone drop down menu in frontend magento?
Here is the code to get all timezones:-
$timezones = Mage::getModel('core/locale')->getOptionTimezones();
echo "<pre>";
print_r($timezones);
echo "</pre>";
You will be getting an array. You can loop through the array and populate the selection/dropdown list like below:-
<select>
<?php
foreach($timezones as $timezone) {
?>
<option value="<?php echo $timezone['value']; ?>"><?php echo $timezone['label']; ?></option>
<?php
}
?>
</select>
Thanks.