how to get the value of the dropdown jquery - php

I want to get the value of the dropdown. I am using msDropDown, but i am getting its value as undefined. The html as follows:
<select name="category[]"
id="webmenus_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>"
onchange="showValue(this.value)"
>
<option value="0" selected="selected" title="Please select hotel category"></option>
<option value="5_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/5star.png"></option>
<option value="4_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/4star.png"></option>
<option value="3_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/3star.png"></option>
<option value="2_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/2star.png"></option>
<option value="1_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/1star.png"></option>
</select>
jquery alert($("input[name='category[]']").val()); alerts as undefined.
How can i get the value of the dropdown?
Thanks,

$(":input[name='category[]']").val()

You use select instead of input
Edit your HTML and set the select Tag to mutiple
<select name="test" multiple>
<option value="1">ITEM 1</option>
<option value="2">ITEM 2</option>
<option value="3">ITEM 3</option>
</select>
alert($('select[name="test"]').val());
// outputs 1,2,3 when you select all three options

try
$("class of select ").change(function() {
alert($(this).val());
});

Related

how to post array varibale in form data with post method

i have one array variable call : $array_variable
$array_variable_1 = array('1','2','3','4' etc ....);
$array_variable_2 = array('1','2','3','4' etc ....);
$array_variable_3 = array('1','2','3','4' etc ....);
$array_variable_4 = array('1','2','3','4' etc ....);
$array_variable_5 = array('1','2','3','4' etc ....);
i want to post this all data by selecting drop down like below:
<select name="fieldforpost">
<option value="<?php $array_variable_1; ?>">id name 1</option>
<option value="<?php $array_variable_2; ?>">id name 2</option>
<option value="<?php $array_variable_3; ?>">id name 3</option>
<option value="<?php $array_variable_4; ?>">id name 4</option>
<option value="<?php $array_variable_5; ?>">id name 1</option>
</select>
and how to get this data in PHP file:
$output_array = $_POST['fieldforpost'];
final word: i can't post this data to my php file any buddy have idea how to do this operation?
Just put echo in values
<select name="fieldforpost">
<option value="<?php echo $array_variable_1; ?>">id name 1</option>
<option value="<?php echo $array_variable_2; ?>">id name 2</option>
<option value="<?php echo $array_variable_3; ?>">id name 3</option>
<option value="<?php echo $array_variable_4; ?>">id name 4</option>
<option value="<?php echo $array_variable_5; ?>">id name 1</option>
</select>
In order to post form fields you need to set an action and post method for the form. For example, if you wanted to post the form to the same page you are on now you would use:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<select name="fieldforpost">
<option value="<?php $array_variable_1; ?>">id name 1</option>
<option value="<?php $array_variable_2; ?>">id name 2</option>
<option value="<?php $array_variable_3; ?>">id name 3</option>
<option value="<?php $array_variable_4; ?>">id name 4</option>
<option value="<?php $array_variable_5; ?>">id name 1</option>
</select>
<input type="submit" name="submit" value="Submit Form">
</form>
After submitting the form the page will reload and $output_array = $_POST['fieldforpost']; should work correctly (assuming a value was selected on the form).
Now, if you want to dynamically populate the options from the array you can do something like this:
<select name="fieldforpost">
<?php
foreach($array_variable as $var) {
echo "<option value=\"$var\">id name $var</option>";
}
?>
</select>
Your values are stored in array so you have to display it using array indexes.
<select name="fieldforpost">
<option value="<?php echo $array_variable[0]; ?>">id name 1</option>
<option value="<?php echo $array_variable[1]; ?>">id name 2</option>
<option value="<?php echo $array_variable[2]; ?>">id name 3</option>
<option value="<?php echo $array_variable[3]; ?>">id name 4</option>
<option value="<?php echo $array_variable[4]; ?>">id name 1</option>
</select>
Remember that in PHP array indexes starts at 0 not 1.
Or you can use the foreach loop:
<select name="fieldforpost">
<?php foreach($array_variable as $variable) : ?>
<option value="<?php echo $variable; ?>">id name <?php echo $var; ?></option>
<?php endforeach; ?>
</select>
If you want to read the form data:
$output_array = $_POST['fieldforpost'];
Make sure that your form has method set to POST. If you leave the method blank, it will use GET.

How do i get the select tag value with codeigniter?

how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');

html select list value not passing on to PHP

I have a form in which values get inserted and passed to PHP, everything was working till I thought about inserting a select value, and this one always gets passed on to the database as 0. This is the form:
<form name="myForm" id="register" action="register.php" method="post" accept-charset="utf-8">
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
This is how I retrieve all my variables, but this select one doesn't seem to work.
PHP:
$value7 = mysql_real_escape_string($_POST['Year']);
Thank you!
Change this
<option value="year1">1</option>
to this
<option value="1">year1</option>
PHP Code
<?php
if(isset($_POST))
echo mysql_real_escape_string($_POST['Year']);
?>
HTML code
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
<input type='submit' >
</form>
change this
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
to
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>

how to create select all option from dropdown?

I am having problem in my search box, actually i have created a search box for multiple selection and it is working properly,
<input type="text" name="search" size=15 maxlength=15 placeholder = "Gene Symbol"/>
<select name="table[]" size = "0" multiple>
<option selected="selected"></option>
<option value="infla_info">Inflammation</option>
<option value="diet_info">diet</option>
<option value="obesity_info">obesity</option>
<option value="stress_info">stress</option>
<option value="athero_info">atherosclerosis</option>
<option value="retino_info">Diabetic Retinopathy</option>
<option value="nephro_info">Diabetic Nephropathy</option>
<option value="neuro_info">Diabetic Neuropathy</option>
</select>
<input type="Submit" name="Submit" value="Gene Search"/>
after this i want to add one more option select all, by which it should show all the records or tables containing query gene.
please help me
try this
Jquery Code:
$('#selectall').click(function() {
$('#countries option').prop('selected', true);
});
Live Demo :
http://jsfiddle.net/3nUPF/177/
You can achieve this using jQuery :
EDIT :- You can select multiple value by pressing Ctrl button of your keyboard and you can get those value using print_r($_POST['countries']);
<select name="countries" id="countries" MULTIPLE size="8">
<option value="all">Select all</option>
<option value="UK">UK</option>
<option value="US">US</option>
<option value="Canada">Canada</option>
<option value="France">France</option>
<option value="India">India</option>
<option value="China">China</option>
</select>
jQuery :
<script>
$(document).ready(function()
{
$('#countries').change(function() {
if($(this).val() == 'all')
{
$('#countries option').prop('selected', true);
}
});
});
</script>
Working Demo

when selecting a value from a dropdown list set other dropdown list to it's default value

I have an php form which contains 7 dropdown lists. When selecting a value from one of this I want the others 6 to return to their default values if they were previously opened. I think it should be used a javascript for this but I don't now to write such a script. I guess each of this dropdown list must have an ID and when I select an option from each one of the 7 the javascript should recognise the id and sets the other to default. I tried using a javascript with document.getElementsByName('id') but it doesn't work.
<form id="form1" name="form1" method="post" action="">
<select name="select" size="1" id="1">
<option value="0" selected="selected">please select</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">red</option>
</select>
<br />
<select name="select2" size="1" id="2">
<option value="0" selected="selected">please select</option>
<option value="1">intel</option>
<option value="2">amd</option>
</select>
<br />
<select name="select3" size="1" id="3">
<option value="0" selected="selected">please select</option>
<option value="1">fish</option>
<option value="2">car</option>
<option value="3">table</option>
</select>
<br />
<select name="select4" size="1" id="4">
<option value="0" selected="selected">please select</option>
<option value="1">lcd</option>
<option value="2">led</option>
</select>
</form>
This is my form.
Let's sey for example I select a value from the second dropdown list "intel". After this i select from the 3rd dropdown list a value "table". What i need to do is when selecting from the 3rd dropdownlist the second one returns to "please select".
The ideea is that using a javascript i do not want those who use the form to select more than one dropdown list at a time.
They should be able to see the values from each one opening the dropdown list but if they open another one the previously must return to the selected="selected" option which is "please select".
Thanks.
Here is a idea to start. Consider 2 dropdown like this.
<select id="Numeric">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="Alphabets">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
Based on the value of 1st dropdown i am selecting the second dropdown
var val = document.getElementById("Numeric").value;
var obj = document.getElementById("Alphabets");
if(val == "1") obj.value = "A"
else if(val == "2") obj.value = "B"
else if(val == "3") obj.value = "C"
Switch is more better
switch(val)
{
case("1"):
obj.value = "A";
break;
case("2"):
obj.value = "B";
break;
case("3"):
obj.value = "C";
break;
}
This is very basic starup idea. You need to implement good logics for your needed functionality
Updated Answer after Comments
<html>
<head>
<script type="text/javascript">
function handleSelect(thisObj){
var list = document.getElementsByTagName("SELECT")
for(var i=0; i< list.length; i++)
{
if(list[i] ==thisObj) continue;
list[i].value = "0";
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<select name="select" size="1" id="1" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">red</option>
</select>
<br />
<select name="select2" size="1" id="2" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">intel</option>
<option value="2">amd</option>
</select>
<br />
<select name="select3" size="1" id="3" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">fish</option>
<option value="2">car</option>
<option value="3">table</option>
</select>
<br />
<select name="select4" size="1" id="4" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">lcd</option>
<option value="2">led</option>
</select>
</form>
</body>
</html>

Categories