Show Checkboxes on select box using jquery in php - php

I have two tables in database: city and local_area
city :
id name
1 FSD
2 LHr
Local_area :
id city_id name
1 1 Kohnoor
2 1 samnabad
3 2 joharabad
4 2 amanabad
<select>
<option value="1">Fsd</option>
<option value="2">LHR</option>
</select>
I have a select box that allows to me to choose the city to display. When I select a city it should show a new HTML element showing all local_area for the given city.
I Have to do this using jQuery, after selection I want to store my record in database table.

<select id="ddCity">
<option value="-1"></option>
<option value="1">FSD</option>
<option value="2">LHr</option>
</select>
<div id="local_area_Grid"></div>
<script>
$(function(){
$("#ddCity").change(function(){
if($(this).val() != -1){
$.ajax({
url: "Server_address",
data:{cityId = $(this).val()},
success: function(data){
var _table = $("<table></table>");
for(var i = 0; i< data.length; i++){
$("<tr></tr>").append($("<td></td>").html("<label><input type='checkbox' value='data[i]' name='local_area'/>" + data[i] + "</label>")).appendTo(_table);
}
$("#local_area_Grid").html("").append(_table);
}
});
}
});
})
</script>
You should return an array from service

You need to use jQuery AJAX to "ask" a server for list of areas corresponding to the city. Or, may be, it would be better for you to load full list of all areas with page. For example:
<script language="JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<select id="city" name="city">
<option value="0">- Select -</option>
<option value="1">Fsd</option>
<option value="2">LHR</option>
</select>
<div id="area" style="display:none">
</div>
<script language="JavaScript">
var areas = {
'1' : { '1' : 'Kohnoor' },
'2' : { '1' : 'amanabad' }
}
$(function (){
var areaSelect = $('#area');
var citySelect = $('#city').change(function (){
var value = $(this).val();
if( value && typeof areas[value] != 'undefined' ){
areaSelect.empty().show();
for( var i in areas[value] ){
areaSelect.append('<input type="checkbox" name="area[]" value="'+i+'" />'+areas[value][i]+'<br />');
}
}else{
areaSelect.empty().hide();
}
});
});
</script>

Related

Multiple else if in jQuery

I am doing dependent select box using jquery, ajax and php from same table of the database. Testpage
$(document).ready(function(){
$('.action').change(function(){
if($(this).val() != '')
{
var action = $(this).attr("id");
var query = $(this).val();
var result = '';
if(action == "name") {
result = 'category';
alert ('test1');
} else if (action == "category") {
result = 'material';
alert ('test2');
} else if (action == "material") {
result = 'source_light';
alert ('test3');
} else if (action == "source_light") {
result = 'color_temperature';
alert ('test4');
} else {
// result = 'color_temperature';
}
$.ajax({
url:"fetch_filter2.php",
method:"POST",
data:{action:action, query:query},
success:function(data){
$('#'+result).html(data);
}
})
}
});
});
I don't know why but it only works: test1 and test2. Test3 and 4 are not sent to php.
HTML file
Maybe something is wrong with select box? But these are just a select box with id's.
<select name="name" id="name" class="form-control action">
<option value="">Select name</option>
<?php echo $name; ?>
</select>
<br />
<select name="category" id="category" class="form-control action">
<option value="">Select category</option>
</select>
<br />
<select name="material" id="material" class="form-control">
<option value="">Select material</option>
</select>
<br />
<select name="source_light" id="source_light" class="form-control">
<option value="">Select source_light</option>
</select>
<br />
<select name="color_temperature" id="color_temperature" class="form-control">
<option value="">Select color_temperature</option>
</select>
What about something like this
$(document).ready(function(){
$('.action').change(function()
{
if($(this).val() != '')
{
var action = $(this).attr("id");
var query = $(this).val();
var result = '';
switch(action) {
case "name":
result = 'category';
alert ('test1');
break;
case "category":
result = 'material';
alert ('test2');
break;
case "material":
result = 'source_light';
alert ('test3');
break;
case "source_light":
result = 'color_temperature';
alert ('test4');
break;
default:
// result = 'color_temperature';
}
$.ajax({
url:"fetch_filter2.php",
method:"POST",
data:{action:action, query:query},
success:function(data){
$('#'+result).html(data);
}
})
}
});
});
Verify your drop down list under "action" class. As per your condition it won't trigger if your selected drop down item doesn't have value if($(this).val() != '').
Also make sure your expected drop down items have the value material for your test3 and source_light for your test4
Another suggestion: To load and trigger dynamic change event for drop down you can use below jQuery API:
$(document).on("change", ".action", function(){
//Your code here
});
If this doesn't solve your problem then please share your HTML or drop down data.
Updated answer: I can see there is no class "action" in your select/drop down items for material, source_light and color_temperature. That is why change actions of these drop down items are not catching in your $('.action').change(function() ... code block.

Dynamic checkbox and update html page

I am trying to make a small webpage doing the following :
1. The user selects a level then depending on the choice, there will be some options appearing as checkboxes.
2. The user can choice one or multiples options
I would like to make appear on the same page dynamically the options he or she chooses and the value associated to this. The value is stored in a mySQL database.
The first step is working but not the second one. If some one could help me ? Once the checkbox is checked or not, the page is not updating.
Here is my test code :
Code HTML
<script>
$(document).ready(function(){
$('#id_class').on('change', function(){
var _class = $(this).val();
if(_class){
$.ajax({
type:'GET',
url:'ajaxData.php',
data:'_class='+_class,
success:function(html){
$('#id_opt').html(html);
}
});
}else{
$('#id_opt').html('<option value="">Complete CP first</option>');
}
});
});
$(document).ready(function(){
$('#id_sci').on('click change', function(){
var _sci = $(this).val();
if(_sci){
$.ajax({
type:'GET',
url:'ajaxDataPrice.php',
data:'_sci='+_sci,
success:function(html){
$('#id_price').html(html);
}
});
}else{
$('#city').html('<option value="">Complete CP first</option>');
}
});
});
</script>
<body>
<form action="test_menu.php" method="POST">
<select id="id_class" name="_class">
<optgroup label="Primaire">
<option value="CP">CP</option>
<option value="CE1">CE1</option>
<option value="CE2">CE2</option>
<option value="CM1">CM1</option>
<option value="CM2">CM2</option>
</optgroup>
<optgroup label="Collège">
<option value="6ème">6ème</option>
<option value="5ème">5ème</option>
<option value="4ème">4ème</option>
<option value="3ème">3ème</option>
</optgroup>
</select>
<input id="id_intern" type="checkbox" name="_intern" value="Yes"> Interne / Externe
<div id="id_opt">
</div>
<br>
<b>Montant à payer :</b>
<div id="id_price">
</div>
<p>
Code PHP
if(!empty($_GET['_sci']))
{
if ($_SESSION['_nbOpt'] > 0)
{
if ($_GET['_sci'] == "Yes")
$_SESSION['_nbOpt'] += 1;
else
$_SESSION['_nbOpt'] -= 1;
}
elseif ($_SESSION['_nbOpt'] == 0)
{
if ($_GET['_sci'] == "Yes")
$_SESSION['_nbOpt'] += 1;
}
$_priceOpt = $_SESSION['_nbOpt']*5;
$_priceTotal = 6 + $_priceOpt;
echo 'Prix matières : 6 € <br>
Prix matières opt : '.$_priceOpt.' € <br>
Prix total : '.$_priceTotal.' € <br>';
;
}
You should use the ajax GET for sending data as below. I think your $_GET might not received the data in php. Once changed the code clear all your browser caches and check.
<script>
$(document).ready(function(){
$('#id_class').on('change', function(){
var _class = $(this).val();
if(_class){
$.ajax({
type:'GET',
url:'ajaxData.php',
data:{'_class':_class}, //changes done here
success:function(html){
$('#id_opt').html(html);
}
});
}else{
$('#id_opt').html('<option value="">Complete CP first</option>');
}
});
});
$(document).ready(function(){
$('#id_sci').on('click change', function(){
var _sci = $(this).val();
if(_sci){
$.ajax({
type:'GET',
url:'ajaxDataPrice.php',
data:{'_sci':_sci}, //changes done here
success:function(html){
$('#id_price').html(html);
}
});
}else{
$('#city').html('<option value="">Complete CP first</option>');
}
});
});
</script>

Display a select option based on other select option

<select name = "team1">
<option>Computer Science</option>
<option>Mathematics</option>
<option>Bioinformatic</option>
<option>Management Sciences</option>
</select>
<select name = "team2">
<option>Computer Science</option>
<option>Mathematics</option>
<option>Bioinformatic</option>
<option>Management Sciences</option>
</select>
how can i use the above code as depending "team2" on what value "team1" have.If i select computer science in the select option of team 1, then i don't want computer science to appear in the options of "team2".
You can give an ID to your selection and check the selection of the specific element. Thus you can distinguish between team1 and team2 and remove some in team2-selection with same value. it could be something like this:
<body>
<select onChange="jsFunction()" id="team1" >
<option onclick="jsFunction()">Computer Science</option>
<option>Mathematics</option>
<option>Bioinformatic</option>
<option>Management Sciences</option>
</select>
<select id="team2">
<option>Computer Science</option>
<option>Mathematics</option>
<option>Bioinformatic</option>
<option>Management Sciences</option>
</select>
<script>
function jsFunction(){
var team1 = document.getElementById( "team1" );
var team2 = document.getElementById( "team2" );
for (i=0;i<4;i++){
team2.remove(0);
}
var position=0;
for (i=0;i<4;i++){
if (i!=team1.selectedIndex){
team2.options[position] = new Option(team1.options[i].value);
position=position+1;
}
}
}
</script>
</body>
You can use Checkbox for these options.Its solves your Problem.
why PHP?
http://jsfiddle.net/655noe6p/
HTML:
<select name = "team1">
</select>
<select name = "team2">
</select>
JS:
var l = [
'Computer Science',
'Mathematics',
'Bioinformatic',
'Management Sciences'
];
$('select').change(function() {
var me = $(this);
var other = me.siblings();
var text = me.children(':selected').text();
other.each(function() {
var select = $(this);
var currentText = select.children(':selected').text();
select.empty();
$.each(l, function(k, d) {
if(d != text) {
select.append($('<option></option>').html(d).attr('selected', d == currentText));
}
});
});
});
$('select').change().change();

Retrieve the districts from database using PostgreSQL

I want to retrieve districts and state from the database, and also to populate second dropdown list based on first dropdown list. In my code below the values are inserted directly:
<!DOCTYPE html>
<head>
<script type="text/javascript" src="C:\Program Files\BitNami WAPPStack\apache2\htdocs \Prj\Online\jquery-1.9.1.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
var options = $('#test2 option');
$('#test1').on('change', function(e) {
$('#test2').append(options);
if ($(this).val() != 'Select') {
$('#test2 option[value!=' + $(this).val() + ']').remove();
} else {
$('#test2').val('Select');
}
});
});
</script>
<form name="form1" method="post" action="fid1.html">
<select name="test1" id="test1">
<option value="Select">Select</option>
<option value="a">TamilNadu</option>
<option value="b">Kerala</option>
<option value="c">Andhra</option>
</select>
<select id="test2" name="test2">
<option value="Select">Select</option>
<option value="a">Chennai</option>
<option value="a">Trichy</option>
<option value="a">Madurai</option>
<option value="b">Trivandram</option>
<option value="b">Cochin</option>
<option value="b">Azhapuzha</option>
<option value="c">Hyderabad</option>
</select>
</form>
</head>
</html>
for that purpose you should use Ajax call on your first drop box .
Ajax is a server side tools to fetch data from database.
Create a ajax function, for example get_country()
$(function() {
$('#test1').change( function() {
var val = $(this).val();
$.ajax({
url: 'findState.php',
dataType: 'html',
data: { country : val },
success: function(data) {
$('#state').html( data );
}
});
}
else {
$('#state').val('').hide();
$('#othstate').show();
}
});
});
now do your database query on findstate.php and use state div to show particuler state list.

Detect which dropdown selected and return its value and ID

I have few drop-down boxes. Is there any way to detect which dropdown was changed using jquery ?
<form id="products">
<select id="Ram">
<option value="0">4 GB</option>
<option value="1">8 GB</option>
</select>
<select id="Hdd">
<option value="0">300 GB</option>
<option value="1">500 GB</option>
</select>
</form>
Javascript/Jquery
$(document).ready(function() {
$('#products').change(function() {
var value = $(this).val(); //In here I want to detect and return the value.
alert(value); // Want to return dropdown ID and its selected value
});
});
$(document).ready(function() {
$('select').not('ids of select tags which you want to exclude seperated by a comma').change(function() {
var id = $(this).attr('id');
var value = $(this).val();
alert('ID ='+id+' Value ='+value);
});
});
Aspirin is right,
I would just improve his answer.
$(document).ready(function() {
$('#products select').change(function() {
var id = $(this).attr('id');
var value = $(this).val();
alert('ID ='+id+' Value ='+value);
});
});

Categories