show a dynamic select input from mysql table - php

I have used a select box to identify the users age group. Accroding to the value of the age group, I need to display diffrent options
For eg.
<select name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
If the user selects the first option I need to display contents of 'tbl_hobbies' (mysql table) with age group 1 in another select box.
<select name="hobbies" id="hobbies">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
For the second option (26-35), I need to display 'tbl_hobbies' contents with age group 2
<select name="hobbies" id="hobbies">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
How can I achieve this using AJAX or is this possible without using AJAX?

First, add an id to age group
<select name="age_group" id="age_group">
Then
$( document ).ready(function() {
$('#age_group').change(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { group_id: $(this).val() },
dataType: "html"
})
.done(function( msg ) {
$('#hobbies').html(msg);
});
});
});
In ajax.php you get all hobbies where group_id = $_POST['group_id']
$sql = "SELECT name FROM tbl_hobbies WHERE group_id = ".$_POST['group_id'];
// Execute your query and put the result in a variable (example $result_query)
// Then loop it
foreach($result_query as $row) {
echo '<option>'.$row['name'].'</option>';
}
NB : The loop may change according to your method to retrieve data

You can simply put all option together and hide option as per user choice
<select name="hobbies" id="hobbies">
<option class="group_1">Cleaning Home</option>
<option class="group_1">Reading</option>
<option class="group_1">Travelling</option>
<option class="group_2">watching movies</option>
<option class="group_2">playing computer games</option>
<option class="group_2">chatting</option>

you can use like this
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
<select name="hobbies2" id="hobbies2">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
and your jquery script look like this
<script>
$('#age_select').on('change'.function(){
var value = $(this).val();
if ( value == '1' ) {
$('#hobbies1').show();
$('#hobbies2').hide();
} else if ( value == '2' ) {
$('#hobbies2').show();
$('#hobbies1').hide();
}
});
</script>
and if you want to pass through ajax
$('#age_select').on('change'.function(){
var value = $(this).val();
var data = 'age='+value;
$.post(
'abc.php',
data
).success(function(resp){
});
});

What you want is called "Dynamic dependent dropdowns". You can view a very good demo here.
Algorithm
1. Get all the options from database // do that using php
2. Assign classes to them according to group // do that using php
3. Put all of them in HTML // do that using php
4. Show/hide on event // do that using jQuery/javascript
Example
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option value="wm" class="grp age-grp-1">watching movies</option>
<option value="pcg" class="grp age-grp-1>playing computer games</option>
<option value="chat" class="grp age-grp-2>chatting</option>
<option value="ch" class="grp age-grp-2>Cleaning Home</option>
<option value="rea" class="grp age-grp-3>Reading</option>
<option value="tr" class="grp age-grp-4>Travelling</option>
</select>
<script type="text/javascript">
// script to execute when you change option from first dropdown
$('#age_select').on('change'.function(){
var ageGrp = $(this).val();
// first, hide all options
$('.grp').hide();
// show options only for selected group
$('.age-grp-'+ageGrp).show();
});
</script>
Hope this helps!!!

Related

PHP Save selection from selection field to get database row [duplicate]

I have used a select box to identify the users age group. Accroding to the value of the age group, I need to display diffrent options
For eg.
<select name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
If the user selects the first option I need to display contents of 'tbl_hobbies' (mysql table) with age group 1 in another select box.
<select name="hobbies" id="hobbies">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
For the second option (26-35), I need to display 'tbl_hobbies' contents with age group 2
<select name="hobbies" id="hobbies">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
How can I achieve this using AJAX or is this possible without using AJAX?
First, add an id to age group
<select name="age_group" id="age_group">
Then
$( document ).ready(function() {
$('#age_group').change(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { group_id: $(this).val() },
dataType: "html"
})
.done(function( msg ) {
$('#hobbies').html(msg);
});
});
});
In ajax.php you get all hobbies where group_id = $_POST['group_id']
$sql = "SELECT name FROM tbl_hobbies WHERE group_id = ".$_POST['group_id'];
// Execute your query and put the result in a variable (example $result_query)
// Then loop it
foreach($result_query as $row) {
echo '<option>'.$row['name'].'</option>';
}
NB : The loop may change according to your method to retrieve data
You can simply put all option together and hide option as per user choice
<select name="hobbies" id="hobbies">
<option class="group_1">Cleaning Home</option>
<option class="group_1">Reading</option>
<option class="group_1">Travelling</option>
<option class="group_2">watching movies</option>
<option class="group_2">playing computer games</option>
<option class="group_2">chatting</option>
you can use like this
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
<select name="hobbies2" id="hobbies2">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
and your jquery script look like this
<script>
$('#age_select').on('change'.function(){
var value = $(this).val();
if ( value == '1' ) {
$('#hobbies1').show();
$('#hobbies2').hide();
} else if ( value == '2' ) {
$('#hobbies2').show();
$('#hobbies1').hide();
}
});
</script>
and if you want to pass through ajax
$('#age_select').on('change'.function(){
var value = $(this).val();
var data = 'age='+value;
$.post(
'abc.php',
data
).success(function(resp){
});
});
What you want is called "Dynamic dependent dropdowns". You can view a very good demo here.
Algorithm
1. Get all the options from database // do that using php
2. Assign classes to them according to group // do that using php
3. Put all of them in HTML // do that using php
4. Show/hide on event // do that using jQuery/javascript
Example
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option value="wm" class="grp age-grp-1">watching movies</option>
<option value="pcg" class="grp age-grp-1>playing computer games</option>
<option value="chat" class="grp age-grp-2>chatting</option>
<option value="ch" class="grp age-grp-2>Cleaning Home</option>
<option value="rea" class="grp age-grp-3>Reading</option>
<option value="tr" class="grp age-grp-4>Travelling</option>
</select>
<script type="text/javascript">
// script to execute when you change option from first dropdown
$('#age_select').on('change'.function(){
var ageGrp = $(this).val();
// first, hide all options
$('.grp').hide();
// show options only for selected group
$('.age-grp-'+ageGrp).show();
});
</script>
Hope this helps!!!

Is it possible to showing unselected options in text box through jquery?

Is it possible to showing selected value from another text box & unselected options to another text box.
For example , I have 2 text boxes & one select box with options(location_1,location_2,location_3).
So when i select option location_2 or any then selected option value will be showing in text_box_1 & remaining unselected options (location_1,location_3) that value will be appearing into another text box is text_box_2.
I have tried to do following ways but only get select option value not unselect.
HTML
<select name="pd_location[]" class="form-control">
<option value="">-- Choose --</option>
<option value="1">Location One</option>
<option value="2">Location Two</option>
<option value="3">Location Three</option>
<option value="4">Location Four</option>
</select>
Jquery
$(document).on('change', '.select_location', function() {
var get_sel_option = $(this).attr("selected_val");
var index= $(".select_location").prop('selectedIndex');
if(index){
var sel_option_test= $('.select_location option ').attr("selected","selected");
}
});
How could i get unselect option values ?
You can use jQuery.map() to get all the options not selected.
Description: Translate all items in an array or object to new array of
items.
$('select[name="pd_location[]"]').on("change", function() {
$('.text_box').val('');
var selected = this.value;
if( selected ) {
$('#text_box_1').val( selected );
var unselected = $(this).find('option:not(:first):not(:selected)').map(function() { return this.value; }).get().join(",");
$('#text_box_2').val( unselected );
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="pd_location[]" class="form-control">
<option value="">-- Choose --</option>
<option value="1">Location One</option>
<option value="2">Location Two</option>
<option value="3">Location Three</option>
<option value="4">Location Four</option>
</select>
<input name="text_box_1" id="text_box_1" class="text_box">
<input name="text_box_2" id="text_box_2" class="text_box">

Switching Dropdown List [duplicate]

This question already has answers here:
Using Ajax, jQuery and PHP in two dropdown lists
(2 answers)
Closed 8 years ago.
I was making a dropdown switch. I mean when I choose an option of the first drop down the second will show specified message preset's in stead of the first.
**Dropdown one**
<select name="bantype">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select></br></br>
**Dropdown two**
<select name="reason">
<option value="Sexual harassment">Sexual harassment</option>
</select>
How can I make that when I select an option from the first dropdown that the option values of the second will switch?
Thank you for help
It's called hierselect, chained or related selects.
I like this jquery plugin: http://www.appelsiini.net/projects/chained
So you have predefined values for each dropdown value from first list
<select id="first">
<option value='ban' data-show="#optionsOne">Ban Ban</option>
<option value='banAll' data-show="#optionsTwo">Ban All</option>
</select>
And you have other (hidden) dropdown for each of your options:
<select id="optionsOne" class='drop'>...</select>
<select id="optionsTwo" class='drop'>...</select>
When #first changes values, show relative dropdown:
$('#first').change(function(){
$('.drop').hide(); // hide all dropdowns
id = $('option:selected', this).data('show'); // take relevant id
$(id).show(); // show that dropdown with above id
});
Call javascript function event onchange,
<select name="bantype" onchange="getoptions(this.value);">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select><br/><br/>
<select name="reason" id="reason"></select>
In your function, use following
getoptions(val) {
if(val == 'user') {
var your_options = '<option value="abc">Abc</option>';
jQuery('#reason').html(your_options);
jQuery('#reason').show(); // if second dropdown is hidden
}
}
If you have a fixed option on page load, then here is one way to achieve what you want.
Give a relation between first select and the second one, like this:
<select name="bantype">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select></br></br>
**Dropdown two**
<select name="reason">
<option class="user" value="a">a</option>
<option class="user" value="d">d</option>
<option class="ip" value="b">b</option>
<option class="system" value="c">c</option>
</select>
Use jQuery to show and hide the option, like this
$("select[name='reason'] option").each(function(){
$(this).hide();
});
$("select[name='bantype']").on("change", function(){
var selected = $(this).val();
$("select[name='reason'] option").each(function(){
if($(this).prop("class") != selected){
$(this).hide();
}else{
$(this).show();
}
});
});
Check out this DEMO..

Clone Div elements and attach events to new elements

I have drop down boxes related to each other. The second dropdown values are filtered according to first dropdown box value.
Now I want to clone elements in div with class="old_div" into div with class="new_div" and add events to each new elements in div with class="new_div".
ADD
<div class="old_div" >
<select name="select1" id="select1">
<option value="1">Fruit</option>
<option value="2">Animal</option>
<option value="3">Bird</option>
<option value="4">Car</option>
</select>
<select name="select2" id="select2">
<option value="1">Banana</option>
<option value="1">Apple</option>
<option value="1">Orange</option>
<option value="2">Wolf</option>
<option value="2">Fox</option>
<option value="2">Bear</option>
<option value="3">Eagle</option>
<option value="3">Hawk</option>
<option value="4">BWM</option>
</select>
<br />
</div>
<div class="new_div">
</div>
I am able to clone elements to new div using Jquery
Jquery code:
$("#add_a").on('click',function(){
var data = $('.old_div').html();
$('.new_div').append(data);
});
$("#select1").on('change', function() {
if($(this).data('options') == undefined){
$(this).data('options',$('#select2 option').clone());
}
var id = $(this).val().trim();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});
But how can i make new elements to be related to each other.
I have created fiddle http://jsfiddle.net/6YEQx/ for reference.
You are using id with select box and making duplicates of it, this is causing problem to identify select box using id in jQuery. So use class instead of id.
HTML :
<select name="select1" class="select1">
<option value="1">Fruit</option>
<option value="2">Animal</option>
<option value="3">Bird</option>
<option value="4">Car</option>
</select>
<select name="select2" class="select2">
<option value="1">Banana</option>
<option value="1">Apple</option>
<option value="1">Orange</option>
<option value="2">Wolf</option>
<option value="2">Fox</option>
<option value="2">Bear</option>
<option value="3">Eagle</option>
<option value="3">Hawk</option>
<option value="4">BWM</option>
</select>
Also you are adding elements dynamically so need to bind change event using $(document) and on.
EDIT: also added new line in add_a button script to show all options in second select box initially.
jQuery :
$("#add_a").on('click',function(){
var data = $('.old_div').html();
$('.new_div').append(data);
// to show all options initially and remove any preselected option
$('.new_div').find('.select2:last option').show().removeAttr('selected');
});
$(document).on('change',".select1", function() {
var id = $(this).val().trim();
$(this).next('.select2').find('option').hide();
var $options = $(this).next('.select2').find('option[value=' + id + ']');
$options.show();
$options.first().attr('selected',true)
});
Demo

how to show textarea based on select dropdown using jquery?

i want to display 2x dropdown menus both will be pre populated (2nd menu "mainToon" will contain over 200 names but for the example i have shown just a few.
<select id="category" name="Category">
<option value=" "></option>
<option value=" ">-----------------</option>
<option value="Main Toon">Main Toon</option>
<option value="Alt Toon">Alt Toon</option>
<option value="Cyno Toon">Cyno Toon</option>
<option value="Super Toon">Super Toon</option>
<option value="Dust Toon">Dust Toon</option>
</select>
<select id="mainToon" name="mainToon">
<option value=" "></option>
<option value=" ">-----------------</option>
<option value="Agmar">Agmar</option>
<option value="S Tein">S Tein</option>
<option value="Karades">Karades</option>
<option value="Bad Kharma">Bad Kharma</option>
<option value="Ed jeni">Ed Jeni</option>
</select>
by default the first dropdown will show blank and i want the "mainToon" dropdown to be hidden untill any of the following are selected:
"Alt Toon",
"Cyno Toon",
"Super Toon",
"Dust Toon"
Then the form will be visable.
Can i do this by applying a .hidden css code to the dropdown and changing the class dynamically when other options are selected?
many thanks for the help.
First hide your dropdown :-
<select id="mainToon" name="mainToon" style="display:none;">
</select>
And use Jquery show hide function like this :-
$(document).ready(function(){
$("#category").change(function(){
var value = $(this).val();
if(value=="Alt Toon" || value=="Cyno Toon")
{
$("#mainToon").show();
}
else
{
$("#mainToon").hide();
}
});
});
Below show the DEMO how to show DIV based on selection value ,
$(function() {
$('#colorselector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
And Here is working demo.

Categories