PHP Save selection from selection field to get database row [duplicate] - 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

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

Dropdown list which excludes value selected in previous dropdown list: PHP

I am new to web development, need help with PHP coding for a form.
A form with 5 dropdown list needs to be created. The values in 1st dropdown list would be "A,B,C,D,E,F,G". If suppose "C" is selected in 1st dropdown, then 2nd dropdown will have all the values except "C" i.e; "A,B,D,E,F,G". If suppose we select "A" in second dropdown, the values shown in 3rd dropdown would be all the values except the value chosen in dropdown 1 & 2. So value in dropdown 3 would be "B,D,E,F,G.
Similarly, it will happen for other two dropdown boxes.
One way you could try is to keep track of what options need to be removed in an array. Then you could define which select controls which. Take this example:
given this html
<select id="a" var="b">
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
<select id="b" var="c" disabled>
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
<select id="c" disabled>
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
you could use this jquery
var doNotShow = [];
$('select').change( function() {
var nextSelect = $(this).attr("var");
doNotShow.push($(this).val());
$.each(doNotShow, function( index, value ) {
$("#" + nextSelect + " option[value='" + value + "']").prop('disabled', true);
});
$("#" + nextSelect).prop('disabled', false);
$(this).prop('disabled', true);
});
doNotShow.push($(this).val()); keeps track of all the previously selected values, then the $.each removes (or in this case, disables) the unwanted options from the next select in line, which is stored in the select's var property.
Here's an example.
This will be done with jquery or even with Javasript, with jQuery you can read the documentation of .change() function, and for Javascript with the onchange Attribute, try to read this question it will help you a little bit : HTML SELECT - Trigger JavaScript ONCHANGE event even when the option is not changed

show a dynamic select input from mysql table

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!!!

Categories