populate dropdown in codeigniter form using set_value - codeigniter

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.

Related

select shows the top record in the db and not the selected

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);?>

YII ajax validation: The error message doesnt appear afer ajax validation

I have 2 fields in the form (num1 and num2), sum of which forms variable, entered to the databases (sum), which is required. There are some other required fields .
array('summ ', 'required'),
In other words in order to get this variable I need two of these fields (num1 and num2). Without ajaxom validation I solve this problem by:
if($_POST['num1'] && $_POST['num2']) {
$model->sum = $_POST['num1'] + $_POST['num2'];
} else {
$model->sum='';
}
Here is code of form’s part:
<?php echo $form->labelEx($model, 'bithday', array('class' => 'label_register')); ?>
<input name="num1" type="text" value="">
<input name="num2" type="text" value="">
<?php echo $form->error($model, 'bithday'); ?>
After forwarding ajax request, the response containing error messages. But the error doesn’t appear. Though errors of other required fields appear correctly.
Response comes in form of json, with key and it’s value. (Data about this error is contained here)
How to solve this problem? .
Use the Yii textfield like so:
<?php echo $form->labelEx($model, 'num1', array('class' => 'label_register'));
<?php echo $form->textField($model,'num1'); ?>
<?php echo $form->error($model, 'num1'); ?>
<?php echo $form->labelEx($model, 'num2', array('class' => 'label_register'));
<?php echo $form->textField($model,'num2'); ?>
<?php echo $form->error($model, 'num2'); ?>
And add the properties $num1 and $num2 to your model.

making a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In your controller you are passing value like
$data['red']=$this->mymodel->get_sec_info();
and in you view you are taking value like
<?php foreach($rec as $row) {?>
So change the vairiable $rec to $red in your view like
<?php foreach($red as $row) {?>
Your option tag is badly formed. The opening option tag is missing its closing bracket, and the closing quotation mark for the option tag's value is in the wrong place.
Corrected:
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>
You should use CI form helper function instead of using html code
In model
function get_sec_info()
{
$records=$this->db->query('SELECT sec_id,name FROM section');
$ret_arr = array();
if($records->num_rows() > 0)
{
while($records->result() as $r)
$ret_arr[$r->sec_id] = $r->name;
}
return $records->result();
}
In your view
echo form_dropdown("section",$rec);
Remove your code below in view:
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
Make sure you have loaded form helper.

Resetting dropdown values after failed validation in CodeIgniter

I have a very simple voting form. It contains a single text field for entry ID, then five dropdowns. The only validation being done is on the text field, but if it fails, I want all of the dropdowns to keep their values.
For example, a user enters 12345 as the entry, then selects option A from all five dropdowns. (Option B is default.) If 12345 fails validation, the dropdowns should still be set to option A when the validation error is displayed. That’s not happening. I’m using the form helper’s form_dropdown to create the dropdowns. Any ideas? As you can see, I tried using set_value on the dropdown called q1, but it doesn’t work. The validation itself does work.
Here’s the code for the form:
<?php
$hidden = array('dot_judge_id' => $this->session->userdata('dot_judge_id'));
echo form_open('vote/create');
$entry_code_data = array(
'name' => 'entry_code',
'id' => 'entry_code',
'value' => set_value('entry_code')
);
echo form_hidden($hidden);
$score_options = array('1'=>'1 (lowest)', '2'=>'2','3'=>'3', '4'=>'4','5'=>'5 (highest)');
?>
<p><label for="entry_code">Entry code: </label><?php echo form_input($entry_code_data); ?></p>
<p><label for="q1">Question 1: </label><?php echo form_dropdown('q1', $score_options, set_value('q1')); ?></p>
<p><label for="q2">Question 2: </label><?php echo form_dropdown('q2', $score_options, ''); ?></p>
<p><label for="q3">Question 3: </label><?php echo form_dropdown('q3', $score_options, ''); ?></p>
<p><label for="q4">Question 4: </label><?php echo form_dropdown('q4', $score_options, ''); ?></p>
<p><label for="q5">Question 5: </label><?php echo form_dropdown('q5', $score_options, ''); ?></p>
<p><?php echo form_submit('submit', 'Submit vote'); ?></p>
<?php echo form_close(); ?>
<?php echo validation_errors('<p class="error">', '</p>');?>
I've had someone on the CodeIgniter forum tell me to use set_select, but I don’t think I can use set_select, because I’m not writing out the individual values of the select box, nor am I trying to provide a hard-coded default value. I need to reset the dropdowns to the values the user selected prior to running the form validation. I’m using the form_dropdown function, so how do I get the actual selected value to pass into the second param of set_select? According to the set_select documentation,
The first parameter must contain the name of the select menu, the
second parameter must contain the value of each item
There was also a comment on this post that said to use set_value -echo form_dropdown('events', $options, set_value('events'), $firstItem);. The comment got three votes, so I'm assuming that it works for some people, but doesn't do anything for me. That question was a year old; perhaps that worked in an older version of CI but not the newest one. In any case, there HAS to be a way to have "sticky" dropdown values when creating dropdowns using form_dropdown, doesn't there? I can't imagine that I'd have to do it the long way and create the dropdown by looping through an array of values and echoing out each option line separately; that kind of defeats the purpose of using something like CI in the first place.
Remember that set_value() requires a second parameter that is the value of each item, so you can get this to work by adding $this->input->post('q1') as the second parameter.
Instead of:
<?php echo form_dropdown('q1', $score_options, set_value('q1')); ?>
You would write:
<?php echo form_dropdown('q1', $score_options, set_value('q1', $this->input->post('q1'))); ?>
Hope that helps.
much easier,
<select name="add_fields_type" id="add_fields_type">
<option value="">Please Select</option>
<option value="Input">Input</option>
<option value="Text Area">Text Area</option>
<option value="Radio Button">Radio Button</option>
<option value="Check Box">Check Box</option>
</select>
<script>
document.getElementById('select_status').value="<?php echo set_value('status'); ?>";
</script>

How to get timezone value

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.

Resources