How to get the select option value in Laravel - php

I have a select option just like below.
<select name = "type">
<option value="">Select one</option>
<option value="1">Abc</option>
<option value="2">Xyz</option>
</select>
I tried to get the value using Input::get('type')But getting the labels Abc. I need to get the value as 1 or 2 or 3 depending on the one that I'm selecting. I even tried this
<select name = "type" onchange="document.getElementById('typeId').value=this.options[this.selectedIndex].value;">
But same thing happened. How can I do this?

Just
document.getElementById('typeId').value=this.value;

Related

Get all selected values from bootstrap multiselect using PHP?

Having an off-day today and can't seem to figure this one out for some reason (feeling like a blonde moment).
I am trying to use the bootstrap (3.3.7) multiselect field
EX:
<select multiple class="form-control" id="select_example">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
</select>
When I submit (submits via ajax), the MySQL db receives all the data, but for multiselect fields, it is only the value of the last selected option in the options list.
How would I go about getting all of the selected option values and putting them into an array or comma separated string?
Selected: 2,4,5
Current value to db: five
Expected: two,four,five (or something of the like)
Is there a PHP solution for this, or jquery (or combo of the two?)
if($_POST['aircraft']){
$_POST['aircraft'] = implode(', ', $_POST['aircraft']);
}
with a select multiselect with the name aircraft the above code converts the returned array of the multiselect, and converts it into a comma separated string.
<select multiple class="form-control" id="select_example" name="options[]">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
$options = implode(',', $_POST['options']);

HTML select Tag Title using Codeigniter 3

I'm using Codeigniter 3.
The countries select box is an optional field on a form:
<select name="countries">
<option>Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
I have the following line for the countries field validation:
$this->form_validation->set_rules('year', 'Year', 'trim|integer');
When I submit the form with the 1st option (Choose a Country), even without a value defined the validator sees something like this:
echo '<pre>';
print_r($this->input->post('year'));
echo '</pre>';
>> Choose a Country
.. when I am expecting something like false or null.
How can I make CI ignore the value when there isn't one selected?
Thanks in advance!
First of all, you paste the wrong line, I think it should be countries instead of year.
You can make it by these ways:
Give the default selection a value, that can be caught. And handle when the value equals to none
<select name="countries">
<option value="none">Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
Change your validation rule that only allows options in the list. If the value returns isn't in the list, it would make error.
$this->form_validation->set_rules('countries', 'Countries', 'required|trim|in_list[en|fr|de]');

how to insert value from select list into mysql database

<select id='selectCat' name='selectCat'>
<option disabled selected value='0'>Select Type</option>
<option value='book'>Book</option>
<option value='txtbook'>Text Books</option>
<option value='notes'>Notes</option>
</select>
Above is the code in html.
I want the user to select one option from the select list and want to store that value into my database. But before storing it into database i am just retrieving the selected value as shown below.
$userCaT = mysqli_real_escape_string($conn,$_POST['selectCat']);
echo $userCat;
But it is showing an empty page.
So plz get me out from this problem.
Thanks.

how to get values from multiple select/listboxes in php?

i want to know that how to get multiple selectbox values in php
means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user
i think an array should be used but i dont know how??
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
like in above example
how do i get know that how many selectboxes were used and how to get value of each box
code for adding the new dropdown
function addRow()
{
var newRow = document.all("tblGrid").insertRow();
var oCell = newRow.insertCell();
oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.
So you can do it something like this:
<select name="myselect[]" id="select1">
<select name="myselect[]" id="select2">
<select name="myselect[]" id="select3">
After form post you can get all select box values:
print_r( $_POST['myselect'] );
For number of select boxes on page you can try:
echo count( $_POST['myselect'] );
Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.

optgroup get in php label associated

I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array

Categories