javascript disable multiselect box based on selections in another multiselect box - php

I have two multi select boxes one for country list and one for state list
<select multiple="multiple" name="country[]" id="country" >
<option value="0">Select a Country</option>
<?php
foreach($country_list as $key=>$value){
echo "<option value=\"$key\"";
if($html['Country Name']==$key|| $row['Country Name']==$key){
echo ' selected="selected"';
}
echo ">$value</option>\n";
}?>
</select>
<select multiple="multiple" name="state[]" id="state">
<option value="">Select a State</option>
<?php
foreach($state_list as $key=>$value){
echo "<option value=\"$key\"";
if($html['state']==$key|| $row['state']==$key){
echo ' selected="selected"';
}
echo ">$value</option>\n";
}?>
</select>
I have this javascript which enables the state box when the country selected is USA.
window.onload = function() {
var selectCountry = document.getElementById('country');
selectCountry.onchange = function() {
document.getElementById('state').disabled = (selectCountry.value != 'USA')? true : false;
};
};
But, I want the state box to be disabled when other countries are selected along with USA in the first one.
Any suggestions please? Thanks

Demo: http://jsfiddle.net/eBTgb/
$("#country").on("click change", function(){
var option_usa = $("option[value=6]", this);
if (option_usa.is(":selected") && option_usa.siblings(":selected").length == 0) {
$("#state").removeAttr("disabled");
}
else {
$("#state").attr("disabled", "disabled");
}
});
I hope understand you correctly.

This can be done using JQuery.
$("#state").hide();
$("#country").change( function () {
var countryName = $("#country").find("option:selected").html();
if(countryName == 'USA'){
$("#state").show();
} else {
$("#state").hide();
}
});
Heres the link to the JSFIDDLE

Related

passing 2 values from dropdown in php using ajax

I have been creating a system as a project on school. I have encountered a problem in getting values using ajax. I wanted to get values from the qselCampus dropdown and the qselCollege dropdown, then pass its values to a php file and use it on a query that will populate the third dropdown. The problem is, I cannot get the right values on the third dropdown. I am just a beginner in PHP and AJAX.
Here is my AJAX code:
$(document).ready(function() {
$("#qselBranch").change(function() {
var branchid = $(this).val();
if(branchid != "") {
$.ajax({
url:"getprof.php",
data:{txtbid:branchid},
type:'POST',
success:function(response) {
var resp = $.trim(response);
//console.log(resp);
$("#qselProf").html(resp);
}
});
} else {
$("#qselProf").html("<option value=''>------- Select --------</option>");
}
});
});
$(document).ready(function() {
$("#qselCollege").change(function() {
var collegeid = $(this).val();
if(collegeid != "") {
$.ajax({
url:"getprof.php",
data:{txtcid:collegeid},
type:'POST',
success:function(response) {
var resp = $.trim(response);
console.log(resp);
$("#qselProf").html(resp);
}
});
} else {
$("#qselProf").html("<option value=''>------- Select --------</option>");
}
});
});
Here is my PHP Code:
<?php
if(isset($_POST['txtbid'])){
$strGetValue = mysqli_real_escape_string($objConn, $_POST['txtbid']);
$strGetValue2 = mysqli_real_escape_string($objConn, $_POST['txtcid']);
echo $strGetValue;
$strStmtGetProf = "SELECT
strTeacherInfoID
, strFirstName
, strMiddleName
, strLastName
, strCampus
FROM tblteachers
WHERE strCampus = '$strGetValue' AND strCollege = '$strGetValue2'";
$objResult = mysqli_query($objConn, $strStmtGetProf) or trigger_error("Query Failed! SQL: Error: ".mysqli_error($objConn), E_USER_ERROR);
echo "<option value=''>------- Select --------</option>";
//echo $strGetValue2;
while($objFetch = mysqli_fetch_array($objResult)){
$strTID = $objFetch['strTeacherInfoID'];
$strFirstName = $objFetch['strFirstName'];
$strLastName = $objFetch['strLastName'];
$strCampus = $objFetch['strCampus'];
//echo $strGetValue;
echo '<option value="' . $strTID . '">' .$strLastName. ", " .$strFirstName. " | Branch: " .$strCampus. '</option>';
}
}
?>
And here is the HTML part:
<div class="form-row col-md-8">
<label>Campus Branch
<select name="qselBranch" id="qselBranch" required class="form-control">
<option value="">------- Select --------</option>
<option value="Bansud">Bansud</option>
<option value="Bataan">Bataan</option>
<option value="Biñan">Biñan</option>
<option value="Cabiao">Cabiao</option>
<option value="Lopez">Lopez</option>
<option value="Mabini">Mabini</option>
<option value="Maragondon">Maragondon</option>
<option value="Mulanay">Mulanay</option>
<option value="Parañaque">Parañaque</option>
<option value="Pulilan">Pulilan</option>
<option value="Quezon">Quezon</option>
<option value="Ragay">Ragay</option>
<!--<option value="Sablayan">Sablayan</option>-->
<option value="San Juan">San Juan</option>
<option value="San Pedro">San Pedro</option>
<option value="Sta. Maria">Sta. Maria</option>
<option value="Sta. Rosa">Sta. Rosa</option>
<option value="Sto. Tomas">Sto. Tomas</option>
<option value="Taguig">Taguig</option>
<option value="Unisan">Unisan</option>
</select>
</label>
</div>
<div class="form-row">
<label>
College/Faculty:
<select name="qselCollege" required class="form-control">
<option value="">------- Select --------</option>
<option value="CAF">College of Accountancy and Finance (CAF)</option>
<option value="CAFA">College of Architecture and Fine Arts (CAFA)</option>
<option value="CAL">College of Arts and Letters (CAL)</option>
<option value="CBA">College of Business Administration (CBA)</option>
<option value="COC">College of Communication (COC)</option>
<option value="CCIS">College of Computer and Information Sciences (CCIS)</option>
<option value="COED">College of Education (COED)</option>
<option value="CE">College of Engineering (CE)</option>
<option value="CHK">College of Human Kinetics (CHK)</option>
<option value="CL">College of Law (CL)</option>
<option value="CPSPA">College of Political Science and Public Administration (CPSPA)</option>
<option value="CSSD">College of Social Sciences and Development (CSSD)</option>
<option value="CS">College of Science (CS)</option>
<option value="CTHTM">College of Tourism, Hospitality and Transportation Management (CTHTM)</option>
</select>
</label>
</div>
Right now, you have 2 different Ajax call and you use both data in your php...even if you send them one by one. First, try to make ONE ajax call when you change a value in your form and if you have two value different than " ". It should look like this :
JS :
$(document).ready(function() {
$("#qselBranch, #qselCollege").change(function() { // On change for each form
var branchid = $("#selBranch").val();
var collegeid = $("#qselCollege").val();
if(branchid != "" && collegeid != "") { // check if both value are not empty
$.ajax({
url:"getprof.php",
data:{txtbid:branchid,txtcid:collegeid}, // send both value in one time
type:'POST',
success:function(response) {
var resp = $.trim(response);
//console.log(resp);
$("#qselProf").html(resp);
}
});
} else {
$("#qselProf").html("<option value=''>------- Select --------</option>");
}
});
});
PHP :
<?php
// I change nothing, juste add a double $_POST condition
if(isset($_POST['txtbid']) && isset($_POST['txtcid'])){
$strGetValue = mysqli_real_escape_string($objConn, $_POST['txtbid']);
$strGetValue2 = mysqli_real_escape_string($objConn, $_POST['txtcid']);
echo $strGetValue;
$strStmtGetProf = "SELECT
strTeacherInfoID
, strFirstName
, strMiddleName
, strLastName
, strCampus
FROM tblteachers
WHERE strCampus = '$strGetValue' AND strCollege =
'$strGetValue2'";
$objResult = mysqli_query($objConn, $strStmtGetProf) or trigger_error("Query Failed! SQL: Error: ".mysqli_error($objConn), E_USER_ERROR);
echo "<option value=''>------- Select --------</option>";
//echo $strGetValue2;
while($objFetch = mysqli_fetch_array($objResult)){
$strTID = $objFetch['strTeacherInfoID'];
$strFirstName = $objFetch['strFirstName'];
$strLastName = $objFetch['strLastName'];
$strCampus = $objFetch['strCampus'];
//echo $strGetValue;
echo '<option value="' . $strTID . '">' .$strLastName. ", " .$strFirstName. " | Branch: " .$strCampus. '</option>';
}
}
?>
Is it what you are looking for? Tell me if it works please :)

Change select dropdown based on first select dropdown?

I am trying to change the second select dropdown based on whether the first selected option contains an (m) or (ft).
My form HTML search is as follows:
<form id="search" action="/property_search" method="get" class="clearfix">
<select name="sqsize" id="sqsize" class="sqsize">
<option value="">sq.ft/sq.m:</option>
<option value="233.52m">233.52m</option>
<option value="233.52m">233.52m</option>
<option value="467.04m">467.04m</option>
<option value="2,513ft">2,513ft</option>
<option value="2,513ft">2,513ft</option>
<option value="5,026ft">5,026ft</option>
</select>
<select name="sqsizemin" id="sqsizemin" class="sqsizemin">
<option value="">Size Min:</option>
</select>
<input type="submit" class="submit" value="Search">
</form>
My Jquery is as follows:
<script type="text/javascript">
$(function(){
$('#sqsize').change(function () {
var options = '';
if($(this).val() == 'm') {
options = '<option value=""></option>';
}
else if ($(this).val() == 'ft') {
options = '<option value="6">6</option>';
}
$('#sqsizemin').html(options);
});
$('#sqsize').trigger('change');
});
Try something like this(using indexOf):
$(function(){
$("#sqsize").change(function(){
var options = '';
if($(this).val().indexOf('m') > -1) {
options = '<option value="">mm</option>';
}
else if ($(this).val().indexOf('ft') > -1) {
options = '<option value="6">6</option>';
}
$('#sqsizemin').html(options);
});
});
Working fiddle: https://jsfiddle.net/robertrozas/b0w61quf/
Try like this:
https://jsfiddle.net/kc5qqtco/
$(function(){
$('#sqsize').change(function () {
if($(this).val().match(/m/g)) {
$('<option value=""></option>').appendTo('#sqsizemin');
}
else if ($(this).val().match(/ft/g)) {
$('<option value="6">6</option>').appendTo('#sqsizemin');
}
});
});
Using indexOf will get you a working solution.
See the jsfiddle

Show/hide dynamic textbox based on pulldown

I'm trying to figure out how to have jQuery show/hide a dynamically generated textbox based on a pulldown menu selection. My JS skills are extremely limited. I have the following created in a while loop, depending on the number of people:
<select name="location[<?=$pid?>]" id="location[<?=$pid?>]">
<option value="<?=$loc_id?>" selected><?=$loc_name?> </option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<? $query_loc = mysqli_query($link, "SELECT * FROM locations WHERE loc_app = 'Y' ORDER BY loc_name ASC");
while($fetch_loc = mysqli_fetch_array($query_loc)){
$loc_id = $fetch_loc['loc_id'];
$loc_name = $fetch_loc['loc_name'];
?>
<option value="<?=$loc_id?>"><?=$loc_name?></option>
<?
} ?>
</select>
<input name="location_other[<?=$pid?>]" type="text" id="location_other[<?=$pid?>]" style="display:none" />
jQuery function:
<script type='text/javascript'>
$(window).load(function(){
$('#location').change(function () {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>
I need to keep the id of the and the serialized with the $pid variable for later processing. Is there another way to call the jquery function on these?
The HTML output looks like this:
<select name="location[287]" id="location[287]">
<option value="1" selected>A</option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<option value="12">B</option>
<option value="12">C</option>
</select>
<input name="location_other[287]" type="text" id="location_other[287]" style="display:none" />
Try
$(window).load(function(){
$('#location').change(function () {
$('input[id^=location_other]').hide();
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other' + $(this).val()).hide();
}
});
});
Try this
<select name="location[287]" id="location[287]">
<option value="Other">Other</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<input name="location_other[287]" type="text" id="location_other" style="display:none" />
<script type='text/javascript'>
$(window).load(function() {
if ($('select[id^="location"]').val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
$('select[id^="location"]').change(function() {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>

Control multiple-select box using a single select box using javascript

`<select multiple="multiple" name="state[]" id="state">
<option value="">Select a State</option>
<?php
foreach($state_list as $key=>$value){
echo "<option value=\"$key\"";
if($html['state']==$key|| $row['state']==$key){
echo ' selected="selected"';
}
echo ">$value</option>\n";
}?>
</select> </p>`
<p>Select A Country
<select name="country" id="country">
<option value="0">Select a Country</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
</select> </p>
I have two select boxes 1)state[] 2) country . What is want is that "If the selected country is not USA, disable the multi-select box which consists all the USA states". Any help is appreciated
Add this Javascript to your code:
<script type="text/javascript">
<!--
window.onload = function() {
var selectCountry = document.getElementById('country');
selectCountry.onchange = function() {
document.getElementById('state').disabled = (selectCountry.value != 'USA')? true : false;
};
};
//-->
</script>
You could do the following if you have jQuery:
$('#country').change(function () {
if($('#country').val() != "USA") {
$('#state').attr('disabled', 'disabled');
}
else {
$('#state').removeAttr('disabled');
}
});
Or if you are using plain JavaScript:
var elem = document.getElementById("country");
elem.onchange = function () {
if(elem.options[e.selectedIndex].value != "USA") {
document.getElementById('state').disabled = "disabled";
}
else {
document.getElementById('state').removeAttribute("disabled");
}
}
This will disable the state combo box when the country combo box contains a value that is not "USA". When the selected country is "USA", it will remove the disabled attribute and make it usable again.
Hope this helps.

onchange event of select box

I am using a selectbox to display the list of "enterpirses" from the database. But the onchange event is not triggering. But when i manually once put the value of querystring then it starts working. I dont understand why it is happening.
I am new to javascript and php, kindly suggest me the solution to this.
<select id="enterprisebox" onchange="javascript:valueselect()" >
<option value="-">-</option>
<?php foreach($enterprise as $val) { ?>
<option value="<?php echo $val['customer_id'];?>"><?php echo $val['customer_name']?>
</option>
<? } ?>
</select>
and my javascript is--
function valueselect()
{
var i = document.getElementById('enterprisebox');
var p = i.options[i.selectedIndex].value;
//alert(p);
window.location.href = "channelinformation.html?selected="+p;
}
onchange="javascript:valueselect()" replace with onchange="valueselect(this.value);"
function valueselect(myval)
{
window.location.href = "channelinformation.html?selected="+myaval;
}
You Can Use Directly
onchange="window.location='channelinformation.html?selected='+myaval"
Make life easier and use jQuery:
$(document).ready(function () {
$('#enterprisebox').change(function () {
window.location.href = "channelinformation.html?selected="+ $(this).val();
});
});
Try this,
<select id="enterprisebox" onchange="javascript:valueselect(this)" >
<option value="-">-</option>
<?php foreach($enterprise as $val) { ?>
<option value="<?php echo $val['customer_id'];?>"><?php echo $val['customer_name']?>
</option>
<? } ?>
</select>
<script>
function valueselect(sel) {
var value = sel.options[sel.selectedIndex].value;
window.location.href = "channelinformation.html?selected="+value;
}
</script>

Categories