i have a 2 types of select list menus:
<select name="category">
<option>----Select----</option>
<option>Category 1</option>
<option>Category 2</option>
<option>Category 3</option>
<option>Category 4</option>
</select>
<select name="category_product">
<option>----Select----</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
</select>
What i liked to to is when i choose "Category 1" of the select list menu, it echos the select list menu "category_product", how can i do this?
I was thinking in put the category_product list menu in a variable, and when i chose a category it echo´s the category_products list menu, but i dont no how to make this detect when the category is chosen.
Hope for some help
Your only way is doing this with JS or jQuery, here is en example doing it with JS,
function showProducts(cat){
document.getElementById(cat).style.display = 'block';
}
<select name="category" onchange="showProducts(this.value)">
<option>----Select----</option>
<option value="category_product">Category 1</option>
<option>Category 2</option>
<option>Category 3</option>
<option>Category 4</option>
</select>
<select style="display:none" id="category_product" name="category_product">
<option>----Select----</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
<option>Product of category 1</option>
</select>
Or another option what you have to make an AJAX call if you want the result should be straight from the database.
UPDATE:
Use this function instead of the above,
function showProducts(cat){
for(var i=1;i<document.getElementsByTagName('select').length;i++){
document.getElementsByTagName('select')[i].style.display = 'none';
};
document.getElementById(cat).style.display = 'block';
}
You can play around with it let me know for any further help.
Related
I need to select both drop down based on the url parameter
Here is my code
<select id="parent-cat" name="parent-cat" onchange="getFilterUrl(this.value)">
<option value="www.site.co/?test=101">Test 1</option>
<option value="www.site.co/?test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat"
onchange="getFilterUrl(this.value)">
<option value="www.site.com/?sample=95">Sample 1</option>
<option value="www.site.co/?sample=96">Sample 2</option>
<option value="www.site.co/?sample=97">Sample 3</option>
<option value="www.site.co/?sample=98">Sample 4</option>
<option value="www.site.co" selected="">Sample 5</option>
</select>
<script>
function getFilterUrl(filterurl) {
var url = filterurl;
if (url) {
window.location = url; // redirect
}
return false;
}
</script>
Right now after selecting each drop down, that is refreshed to selected url,
I am looking for code, if we select first drop down, and next one,
I need to form url like below for second drop down first option.
Example: if Test 1 is selected, then, Sample 1 selected, the Url should be like beloww with both option as selected
site.co/?test=102&sample=95
How this can be done?
Please anyone help me to achieve this.
Thanks
As I said, your question is not clear
Here is a fixed version of your code.
document.getElementById("container").addEventListener("change",function(e) {
var parent = document.getElementById("parent-cat").value,
child = document.getElementById("child-cat").value;
if (parent && child) {
var url = "https://site.co?"+parent+"&"+child;
console.log(url)
// window.location=url;
}
});
<div id="container">
<select id="parent-cat" name="parent-cat">
<option value="">Please select</option>
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat" >
<option value="">Please select</option>
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
</select>
</DIV>
In PHP:
document.getElementById("container").addEventListener("change", function(e) {
var parent = document.getElementById("parent-cat").value,
child = document.getElementById("child-cat").value;
document.getElementById("message").innerHTML= parent && child ? "":"Please choose both";
if (parent && child) document.getElementById("myForm").submit();
});
<form action="redirect.php" id="myForm" method="post">
<div id="container">
<select id="parent-cat" name="parent-cat">
<option value="">Please select</option>
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat">
<option value="">Please select</option>
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
</select>
</div>
</form>
<span id="message"></span>
using
$parent = isset($_POST['parent-cat']) ? $_POST['parent-cat'] : null;
$child = isset($_POST['child-cat']) ? $_POST['child-cat'] : null;
if ($parent && $child) {
header("location: https:///www.site.co?".$parent."&".$child);
}
If you don't want to redirect immediately after choosing anyone just erase one of the onchange event.
HTML:
<select id="parent-cat" name="parent-cat" onchange="getFilterUrl()">
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat"
onchange="getFilterUrl()">
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
<option value="www.site.co" selected="">Sample 5</option>
</select>
Script:
function getFilterUrl(filterurl) {
var s1 = $('#parent-cat').val();
var s2 = $('#child-cat').val();
if (s2 == 'www.site.co') {
s2 = '';
}
window.location.replace("http://www.site.co/?"+s1+'&'+s2);
return false;
}
In case of 5th option from the second select I exclude the second param from the url. If you have any questions - ask them.
I'm making a learning management system for the students. On the signup page I'm asking the student to tell me how many number of courses you're enrolling in this semester so that according to that i give him the number of list of teachers to save to his subject.
The below jquery code is running okay and updating the values when the student wants to select a subject. But i need something more accurate. This code only shows the number of lists that are pre written in it. I want to make it dynamic that if the student selects taking '2' courses then only two of these list items should show up there. Any body can help?
The jquery and html code is
<select name="select1" id="select1">
<option value="1">Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
<option value="4">Semester 4</option>
</select>
<select name="select2" id="select2">
<option value="1">name of Subject 1 from semester 1</option>
<option value="1">name of Subject 2 from semester 1</option>
<option value="1">name of Subject 3 from semester 1</option>
<option value="2">name of Subject 1 from semester 2</option>
<option value="2">name of Subject 2 from semester 2</option>
<option value="2">name of Subject 2 from semester 2</option>
<option value="3">name of Subject 1 from semester 3</option>
<option value="3">name of Subject 1 from semester 3</option>
<option value="4">name of Subject 1 from semester 4<option>
</select>
and
$("#select1").change(function() { if($(this).data('options') == undefined){
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options',$('#select2 option').clone());
} var id = $(this).val(); var options = $(this).data('options').filter('[value=' + id + ']'); $('#select2').html(options); });
For speedy checking you can see the running code here on JSFiddle
Basically, I have two dropdown lists on my page, both query the same database to populate themselves. What I want is for a use to be able to select an item in one of the dropdown lists, and then for the second dropdown list to show every other option except for what the user selected in the first dropdown list.
I don't want the page to be refreshed in the process, and I want to avoid Sessions/Cookies if possible.
The easiest way I can think to do this is by setting a variable when the user selects a thing in one of the lists, but I can't for the life of me figure out how to do this.
HTML sample
<select id="select1" >
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<select id="select2" >
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
Javascript
$("#select1").change(function() {
var val = $(this).val();
$('#select2').children('option[value="' + val + '"]').attr('disabled', true);
// or
$('#select2').children('option[value="' + val + '"]').remove();
})
JSFIddle sample
I want a form to have multiple drop down selects that change based on the previous selection. This is how I want it to work.
<select id="format">
<option selected="selected" value="NULL">-- Select a product Format --</option>
<option value="PS3">PS3</option>
<option value="Xbox 360">Xbox 360</option>
<option value="Wii U">Wii U</option>
</select>
So if you select PS3 you are then faced with more options for PS3 product type
<select id="ps3-product-type">
<option selected="selected" value="NULL">-- Select a product PS3 product type --</option>
<option value="chargers">chargers</option>
<option value="controllers">controllers</option>
<option value="headsets">headsets</option>
</select>
So then you select headsets for example and you get another set of final options of products
<select id="ps3-headsets">
<option selected="selected" value="NULL">-- Select a PS3 headset --</option>
<option value="product-1">product 1</option>
<option value="product 2">product 2</option>
<option value="product 3">product 3</option>
</select>
How can I do this with jquery or PHP? Please bare in mind that I will also have selects for Xbox 360 and Wii U too.
UPDATE PROGRESS:
http://jsfiddle.net/maximus83/r7MN9/639/
Here is my HTML, I have got the first set of arrays working for selecting product type, But I cant get the 3rd box working, I need 3rd box to say the product, where do I go from here.
<select id="cat">
<option val="PS3">PS3</option>
<option val="Xbox360">Xbox360</option>
<option val="WiiU">WiiU</option>
<option val="Multiformat">Multiformat</option>
</select>
<select id="item">
</select>
<select id="product">
</select>
Here is my script
PS3=new Array('Headsets(PS3)','Controllers(PS3)','Chargers(PS3)','Cables(PS3)');
Xbox360=new Array('Headsets(360)','Chargers(360)');
WiiU=new Array('Controllers(WiiU)','Headsets(WiiU)');
Multiformat=new Array('Headsets(Multi)','Chairs(Multi)','Cables(Multi)');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
eval(cat).forEach(function(t) {
$('#item').append('<option val="#item">'+t+'</option>');
});
}
This should do the trick for you. It's a previously answered thread on the exact same topic - how to populate a drop down based on the value in another. It takes advantage jQuery (And I've used it before myself as reference).
I have a problem with a dropdownlist created in Yii. I wanted to create a dropdown list with sample below:
<select id="con_id">
<option value="1">Con 1</option>
<option value="2">Con 2</option>
<option value="3">Con 3</option>
<option value="4">Con 4</option>
<option value="5">Con 5</option>
<option value="6">Con 6</option>
<option value="all">--All--</option>
</select>
Dropdown menu list are obtained from the database. Below is the sample code that I created in Yii format:
echo $form->dropDownList($model, '_conId', array_merge(CHtml::listData(Consultant::model()->findAll(), 'id', 'name'), array('client_id' => '--All--')));
However, the result of the dropdown list created in Yii become like this:
<select id="con_id">
<option value="">Con 1</option>
<option value="1">Con 2</option>
<option value="2">Con 3</option>
<option value="3">Con 4</option>
<option value="4">Con 5</option>
<option value="5">Con 6</option>
<option value="all">--All--</option>
</select>
The value on the option is not correctly assigned. Can anyone help me?
UPDATE:
Somehow I have found the solution:
echo $form->dropDownList($model, '_conId', array('all' => '--All--') + CHtml::listData(Consultant::model()->findAll(), 'id', 'name'));
There's an 'empty' option for CHtml::listData, where you can set the text for the 'empty' option.
Here's an example using your code:
<?php echo $form->dropDownList(
$model,
'_conId',
CHtml::listData(
Consultant::model()->findAll(),'id','name'), // list all data from model
array('empty'=>'--All--') // set empty data label
); ?>