how to get values from multiple select/listboxes in php? - 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.

Related

How to get the select option value in Laravel

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;

retrieve selected value in form_dropdown() and act on it in the same view

I am using codeigiter's form_dropdown() and would like to make the selection alter data farther down in the same view without submitting the form.
In the view php file:
echo form_dropdown('department_select',$options,'1');
<?php $test_list = $this->trainingmodel->get_dept_tests($deptselected); ?>
I would like $deptselected to reflect what the user has selected in the form_dropdown(). I would like this to update every time the user changes their dropdown selection but without submitting the form.
Things like the following would work if the form was submitted, but not before:
$deptselected = $_POST['department_select'];
or
$deptselected = $this->input->post('department_select');
There must be a way to do what I want using javascript, or onchange or the 4th input parameter to form_dropdown().
You can use the chained select jquery plugin http://www.appelsiini.net/projects/chained
A simple example taken from the website:
<select id="mark" name="mark">
<option value="">--</option>
<option value="bmw" selected>BMW</option>
<option value="audi">Audi</option>
</select>
<select id="series" name="series">
<option value="--">--</option>
</select>
<select id="engine" name="engine">
<option value="--">--</option>
</select>
<select id="transmission" name="transmission">
<option value="--">--</option>
</select>
And on
$("#transmission").remoteChained({
parents : "#engine",
url : "/api/transmissions.json",
depends : "#series"
});
You can set the ajax url to your Codeigniter controller that will serve the data to the view in json format.

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

How to get multiple drop-down box value using J-Query

I have generated drop-down box using loop. Now I want to get all value using J-Query for pass these value for insertion through Ajax.
My HTML CODE is:
<select name="travel[]" id="travel[]" >
<option value="1">Car</option>
<select>
<select name="travel[]" id="travel[]" >
<option value="2">Train</option>
<select>
<select name="travel[]" id="travel[]" >
<option value="3">Bus</option>
<select>
After click on Save button, how to collect all values of travel?
I don't understand why you define three <select> elements with an index-based name, each containing a single <option>. You do know that a <select> element is exactly that - it gives the user a selection?
With your current markup, the user doesn't get a choice about their selection - rather, travel[] will always be an array containing: 1,2,3.
You'd be better to modify your markup as follows:
<select name="travel" id="select_travel">
<option value="1">Car</option>
<option value="2">Train</option>
<option value="3">Bus</option>
<select>
Now you can easily access the value using:
$('#select_travel').val();
You can do this:
var data = $('select[name="travel[]"]').map(function () {
return $(this).val();
}).get();
console.log(data); // ["1", "2", "3"]
This will return you an array with the value for all the dropdowns.
Fiddle demo
why you are making name and id both as array type... i think one will enough if you really want to use array type.... then make id as unique for all...
<select name="travel[]" id="abc">
</select>
<select name="travel[]" id="def">
</select>
<select name="travel[]" id="xyz">
</select>
now you can access dropdown list easily..
$("#abc").val();
$("#def").val();
$("#xyz").val();
or else you can define uniq class for them if you really want array type id and name
<select name="travel[]" id="travel[]" class="abc">
</select>
<select name="travel[]" id="travel[]" class="xyz">
</select>
and try like this...
$(".abc").val();
$(".xyz").val();
hope it may help you
you should try js like-
var selectOption=$('#select_travel').val()
var Id=selectOption.getAttribute('id');
var name=selectOption.getgetAttribute('id');
now you can take output on button click

change browser href in combobox option

I try to make a list of data from a table in a database. I do this with an select option, because the user is only allowed to choose one.
I do it on this way:
<select name="test">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php" onClick="window.location.href='other.php'">
Other
</option>
</select>
This works in IE and Firefox, but not in Google Chrome.
It's not an option to put the onClick in the <select>-tag, because they don't have a href and I need their values to get the primary key and store something in the table.
I hope you can help me.
UPDATE: this is the solution for my case, solved by Harry
<select name="test" onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
Try the below code and check if it solves your problem. You needn't have the href values in the select tag for this to work. The href can be in your option's value tag.
UPDATE: The below code would do the redirect only if the value of the selected option tag has .php in it. For the first two options, no action will be taken.
<select name="test"
onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
this isn't valid HTML. it should at least be:
<option value="other.php" onClick="window.location.href='other.php'">
try this
<select name="test"
onchange="var val = this.value;
if (val.indexOf('other.php')!=-1) window.location=loc">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>

Categories