Dropdown always lists all values in codeigniter - php

My dropdown always lists all values all are visible every time.
view
<label>Choose Employees</label>
<select name="select_employee[]" multiple="multiple">
<option disabled="disabled" selected>Select</option>
<? foreach ($employee->result() as $var)
{?>
<option value="<?echo $var->emp_id;?>"><?echo $var->emp_name;?></option>
<?}?>
</select>
This is the permanent view. Is this a css issue..?
Removing the array symbol it exists proper view..but I want to keep it as an array.
initially vie this way

<select name="select_employee[]" multiple="multiple" size="1"> will display two options.
Default value is 1 for single select. If the multiple attribute is present, the default value is 4

You have multiple="multiple" in your code, which makes all the options visible because your user needs to be able to select more than one option.
Here is the documentation: http://www.w3schools.com/tags/att_select_multiple.asp

Related

How to access select boxes as array

I have four fields, out of them 3 is textboxes and 1 is dropdown. I am cloning them dynamically whenever user clicks on add more.
For textboxes i have given its name as array and its working fine (name="xyz[]").
Now the problem I am facing is with dropdown. I have tried same way to deal with it but its not working it seems. I can't access the values through PHP, it keeps giving me the values of the first select box only.
<div class="cust-input">
<select id="head_of_institute" name="head_of_institute[]">
<option>Select Management</option>
<option value="Principal">Principal</option>
<option value="Director">Director</option>
<option value="Other">Other</option>
</select>
<select id="head_of_institute" name="head_of_institute[]">
<option>Select Management</option>
<option value="Principal">Principal</option>
<option value="Director">Director</option>
<option value="Other">Other</option>
</select>
</div>
<div class="cust-input">
<input type="text" name="head_name[]">
<input type="text" name="head_name[]">
</div>
Can you please suggest how to solve this problem?
Thanks in advance
Your dropdown is not working only because, I'm sure that you are adding new dropdown with the same ID.
Yes, ID must be unique on a page. If you add same ID more than one then javascript will ignore another.
So, my suggestion is to add new dropdown with name="head_of_institute[]" and id="head_of_institute_1" here 1 can be a counter for next downdown it'll be head_of_institute_2.
:) enjoy!

PHP associate form fields with other fields

I have a form that will be used for quoting items. The user will be able to add blocks of fields to the form which I have working at the moment. Consider the code below
<div class="fields">
<select name="landscaping[]">
<option value="1">Rocks</option>
<option value="2">Other Rocks</option>
</select>
<select name="veneer[]">
<option value="1">Veneer Rocks</option>
<option value="2">Other Veneer Rocks</option>
</select>
<input type="text" name="quantity[]">
</div>
The problem is the user may select either veneer or landscaping but not both but no matter what there will be a quantity associated with the choice. And likely an additional parameter. I need a way to associate quantity with veneer or landscaping in each instance of this section.
Since you are using javascript to hide one of the elements at a time, I would also include a hidden field which holds the value of the select field which you want to grab the value from. You could also just look for the one which has a value, but since they can probably each have a value, it's safer to send a value which tells you for sure which field you should pull from.
<div class="fields">
<select name="landscaping[]">
<option value="1">Rocks</option>
<option value="2">Other Rocks</option>
</select>
<select name="veneer[]">
<option value="1">Veneer Rocks</option>
<option value="2">Other Veneer Rocks</option>
</select>
<input type="text" name="quantity[]">
<!-- This value should change depending on which one is currently visible -->
<input type="hidden" name="selectedField" value="landscaping">
</div>
Then in your form processing page use something like the following:
$selection = $_POST[$_POST['selectedField']];
This will pull the value of your selectedField post dynamically and use it to then grab the correct select list. Using this method can be risky though... Make sure that you sanitize your POST data!

Is there something like readonly property for a dropdown?

My form contains a radio-button & a drop-down. So depending upon the selected value of radio button i need to enable/disable the drop-down.
I am doing this:
<select name="bas_type" id="bas_type" disabled="disabled">
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Annualy">Annualy</option>
</select>
But i want to get drop-down value as empty in $_POST if form is disabled..
So this won't work. Please any suggestions..
Simply add a hidden field with the value you want to receive when the control is disabled and the same name as the dropdown. Be sure to place it before the dropdown in the HTML:
<input type="hidden" name="bas_type" value="" />
<select name="bas_type" id="bas_type" disabled="disabled">
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Annualy">Annualy</option>
</select>
Of course you shouldn't really have to do this, since in PHP you should be receiving incoming parameters with at least a helper function like
function param($name, $default = null) {
return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
}
Don't rely on data you get from form. What you should actually do is check serverside if radiobutton is active or not, and depending on that ignore or accept value from dropbox.
Set the value in the form to "" and that should do the trick:
<option value="">Annualy</option>
or use a space like this (not sure which will work better for you, haven't tried to do it):
<option value=" ">Annualy</option>
Edit: The data contained in the value="something" is the actual data that is passed to the URL via a get. Hence, if you blank it out, the data won't get passed.

2 select boxes with same id's but one hidden

I've 2 select boxes with same id's as well as names but at a time only one is shown.
But when form is posted i do not get the exact value.It only displays the first value from the dropdown.
Following is a sample code of the same.
<?php
var_dump($_POST)
?>
<form method="POST" action="">
<select name="test" id="test">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="test" id="test" style="display:none" >
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="Add"/>
</form>
I even tried surrounding the select box with a div and then hidding it.
Not sure why you have two select's with the same name, but when you hide one you also need to disable it too (disabled="disabled").
As per HTML specification, disabled fields are not submitted so you should have your true value posted from the select that isn't disabled.
Presumably you're using some javascript to switch which select is displayed?
If that's the case, then start off with
<select name="test" id="test"></select>
<select name="test_dummy" id="test_dummy" style="display:none" ></select>
And whichever code changes the style attribute will also have to change the name and id attributes
Even when one of these is hidden at every point, they should have different names in the markup so you can identify which one is which.
Otherwise it'll pass only the last element's (in the markup) value through the form (no matter if it's hidden via CSS or not).
I'm not quite sure why your hiding/showing only one of these at a time anyway. It'd likely be better if you replaced the <option>'s inside the select to the new ones, instead of using show/hide.
You would have to have different id's and in your code conditionally check which select box value is visible fetch it's value.
When you submit a form, the POST data contains data basing on the attribute name. If they have the same name, the value of the second select overwrites the value chosen from the first one. Even if you hide it in the page.
If you want to have just the value of the first select, you should disable the second one, by adding disabled="disabled" or by changing its name.

How to have a default option selected in a dynamic pull down menu?

I have a drop down menu that I am dynamically populating based on values in a database table. The code looks something like this:
<select>
<option value="" selected="selected">Select Option</option>
<?php foreach($var as $row): ?>
<option value="<?php echo $row->value;?>"><?php echo $row->value;?></option>
<?php endforeach;?>
</select>
This works fine and creates a default option called 'Select Option' at the top of the list. But I also have another variable that I am passing to this view that specifies a custom default option. So rather than having 'Select Option' as the default selected option, it would be one of the dynamically printed values in the middle of the list? Any ideas on how to do this (no javascript)?
check with an if-else construct in your foreach if the current option is the default one. If so, also print selected="selected"

Categories