Get value from multiple select dropdown on Yii to be inserted to database - yii

Help I want to get value from a multiple select dropdown from a form to a controller.
Here's my form:
<select multiple="multiple" id="form-field-select-4" class="form-control search-select" name="tim_teknis">
<option value=""> </option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
</select>
And here's my controller:
$tim_teknis = $_POST['tim_teknis'];
It turned out to be showed like this: "tim_teknis" not the value of the dropdown. I hope anyone could understand what I mean. Thank you!

You have to implode the values before insert

May be your form not method is 'POST' , so "tim_teknis" not the value of the dropdown.
Solution:
<form action="submit_form.php" method="POST"><select multiple="multiple" id="form-field-select-4" class="form-control search-select" name="tim_teknis[]"><option value=""> </option><option value="AL">Alabama</option><option value="AK">Alaska</option></select></form><?php if (!empty($_POST['tim_teknis'])) {var_dump($_POST['tim_teknis']);}?>
Result after submit form:
array(2) { [0]=> string(2) "AL" [1]=> string(2) "AK" }

Related

Laravel - capture all input names starting with category_id at controller?

In my view I have several category_id inputs.
<select id="category_select1" class="form-control" name="category_id1">
#foreach($category as $c)
<option value="{{$c->id}}">{{$c->name}}</option>
#endforeach
</select>
<select id="category_select2" class="form-control" name="category_id2">
#foreach($category2 as $c)
<option value="{{$c->id}}">{{$c->name}}</option>
#endforeach
</select>
There might be more selectboxes like this so I want to get all of them no mater how many selectboxes there. All the selectbox names will be like category_id1, category_id2.. etc.
$request->input('category_id.*');
But this returns null. What am I doing wrong?
You should use arrays:
#foreach($categories as $i => $category)
<select id="category_select{{$i}}" class="form-control" name="category_id[]">
#foreach($category as $c)
<option value="{{$c->id}}">{{$c->name}}</option>
#endforeach
</select>
#endforeach
and in the controller simply use:
$categories = $request->input('category_id');
foreach($categories as $category_id) {
// ...
}
$request->input('category_id') will be an array of ids
Your solution (different named inputs) is not much reliable.
In any case if you want to use that,
you can use the collections to filter the request inputs by the name to get the categories' ids
Eg.
use Illuminate\Support\Str;
...
$categories = collect($request->all())->filter(function($value, $key) {
return Str::startsWith($key, 'category_id');
})->toArray();

How to make materializecss dropdown appear below selectbox?

I am using materilizecss and i have the following dropdown:
<select class="txtInput" data-val="true" data-val-number="The field SelectedAssetCategoryId must be a number." data-val-required="Select a asset category" id="ddlAssetCategory" name="SelectedAssetCategoryId"><option value="">Asset Category</option>
<option value="1">Disease State/Condition Training</option>
<option value="5">Hot Topics Training</option>
<option value="6">Launch Training</option>
<option value="7">Medical Meetings</option>
<option value="2">Product Training</option>
<option value="8">Quiz</option>
<option value="4">Real World Evidence/HEOR Training</option>
<option value="3">Treatment/Competitive Landscape Training</option>
</select>
I was initilizing it like so:
$("select").not('[multiple="multiple"]').formSelect({
});
But what i wanted was the dropdown to show below the selectbox not above , so i added the below line of code:
$("select").not('[multiple="multiple"]').formSelect({
belowOrigin: true
});
But even this does't seem to work , What option can i add so that the dropdown menu shows below the select box ?
Use This :
$("select").not('[multiple="multiple"]').formSelect({
dropdownOptions: {coverTrigger: false}
});
Instead of :
$("select").not('[multiple="multiple"]').formSelect({
belowOrigin: true
});
JSFiddle
Actually with dropdownOptions you can pass dropdown options to select form.

How to Reset Select Drop down in VueJs

I am new in vue js please help.
I have two dropdowns and that that dropdown create from loop
<select v-if="tour.city_expert == 2" v-model="selected[index]" class="form-control" :id="'city_expert_both'+index" v-on:change="intiTourCityExpert(index, $event)" :disabled="tour.is_disable==true">
<option value="" selected>Select City Expert</option>
<option value="Guideinperson">Guide in person</option>
<option value="AudioGuide">Audio Guide</option>
</select>
I as per image I want reset dropdown if I uncheck the checkbox but only reset single row as I uncheck the checkbox.
I set v-model="selected[index]" on dropdown and set code on uncheck this.$set(this.selected, index, ''); but its not working.
Help
I ran into similar situation, and following worked for me.
And to rest try following anywhere - this.selectedStatus = '0';
<select id="item_status" class="form-control" v-model="selectedStatus">
<option value='0'></option>
<option value='1'>One</option>
<option value='2'>Two</option>
</select>
If I were you, I would do something like this:
Add an onchange event to the checkbox with its index:
#change="onChange(index)"
In the methods object define the function
onChange like this:
methods: {
onChange(index) {
this.selected[index] = ''
}
}
If you want to reset the value of dropdown on click on any other button or so, than just set it to null.
this.accountType = null;
My Dropdown:
<ejs-dropdownlist
v-model:value="accountType"
:dataSource="accountTypesData"
:select="accountTypeSelect"
placeholder="Select a type"
></ejs-dropdownlist>

mvc - generating report - selecting ALL datatable items in dataset using dropdown select

this is my controller:
public ActionResult GenerateReport(string Employeename, int year)
{
EmployeeLeave ds = new EmployeeLeave();
EmployeeLeaveTableAdapter da = new EmployeeLeaveTableAdapter();
da.Fill(ds._EmployeeLeave, Employeename, year);
....
}
this is the code snippet in my index.cshtml:
<form action="/Report/GenerateReport/" method="post">
<select id="Employeename" name="Employeename" class="form-control form- control-lg">
<option value="" disabled selected>Select Name</option>
<option value="ALL">ALL</option>
#foreach (var item in ViewBag.userlist)
{
<option value="#item.username">#item.lastname</option>
}
</select>
<select id="year" name="year" class="form-control form-control-lg">
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>
<button type="submit" value="Generate Report" class="btn btn-primary">Generate Report</button>
</form>
and this is the sql statement snippet in my dataset table adapter:
WHERE (tblfile_leave.EMP_NAME = #username AND tblfile_leave.YEAR = #year)
it gets whatever the value that is in the select option. however, when i select "ALL" in the dropdown, i'm getting no results at all. how can i display all the result in the select statement in connection to the dropdown option "ALL"
If you are passing integer type to your controller function, it will generate javascript error on selecting Select All option as it has value = All which is a string not an integer. You can see this using some browser tools on console window.
One solution is to render option tag for select All as:
<option value="-1">Select All</option>
This will pass -1 to your controller function. Where you can check like:
if (value.Equals(-1))
{
//Your logic to select all records from database
}
Hope this helps.

How to get option text from a dijit.form.Select?

I have a dijit.form.Select on my page:
<c:set var="qualId" value="${previous.qualification.id}" />
<select id="qualification" name="qualification" dojoType="dijit.form.Select" onchange="checkForPHD()">
<option value="-1" label=" "> </option>
<c:forEach items="${requestScope.qualifications}" var="qualItem">
<c:choose>
<c:when test="${qualId eq qualItem.id}">
<option value="${qualItem.id}" selected = "selected">${qualItem.name}</option>
</c:when>
<c:otherwise>
<option value="${qualItem.id}">${qualItem.name}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
Then some javascript that I'm trying to use to set some text to the TEXT of the option chosen from the select box;
function checkForPHD() {
dojo.byId('clazzPHDMessage').innerHTML = dojo.byId('qualification')
.attr('displayedValue');
}
I'd read that the .attr('displayedValue') was suppose to get the text from the selected option in a dijit.form.Select but it doesn't seem to do much? .attr('value') got that values ok but I need the TEXT?
You should use dijit.byId() to get widget instance. Try to use this code to get selected text:
dijit.byId('qualification').attr('displayedValue')
Seems like what you want is the innerHTML of the currently selected option (<option>THIS TEXT??</option>). I think this should do the trick. Loop over all the select's options with getOptions and find the selected one, then return its innerHTML. I don't know if dijit.form.Select has a selectedIndex property, but that would help too.
function getSelectedText() {
dojo.foreach(dijit.byId("qualification").getOptions(), function(opt, i) {
if (opt.selected) {
return opt.innerHTML;
}
});
}
You should try to get the selected node first, then get the attribute you want, as follows:
dijit.byId("qualification").getSelected().attr('innerHTML');
You can try the below Code Snip
dijit.registry.byId("regionList").value;
<select name="regionList" id="regionList" data-dojo-id="regionList" data-dojo-type="dijit/form/Select">
<option value="" selected="true">Select LoB</option>
<option value="Asia" >Asia</option>
<option value="Africa" >Africa</option>
</select>