`<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.
Related
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 :)
I am submitting a form using jquery and ajax. I have my first select menu and depending on the value of this select menu a div that contains a different menu will become visible.
<table>
<tr>
<td>
<select required="" class="input-medium" id="foulTypes" name="foul" data-original-title="" title="">
<option value="">Select Foul</option>
<option value="85">AID - Aiding Runner</option>
<option value="1">BAT - Illegal Bat</option>
<option value="2">BBW - Block Below Waist</option>
<option value="3">BOB - Blocking Out of Bounds</option>
<option value="4">CHB - Chop Block</option>
</select>
</td>
</tr>
</table
<script>
var foulTypes = $('#foulTypes');
var select = this.value;
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
} else {
$('#catDPI').hide();
}
if ($(this).val() == '85') {
$divSelectedCategory = $("#catILF");
$('#catILF').show();
} else {
$('#catILF').hide();
}
if ($(this).val() == '2') {
$('#catOPI').show();
$divSelectedCategory = $("#catOPI");
} else {
$('#catOPI').hide();
}
//We now enable only for the selected category
$divSelectedCategory.show().find(".category").attr("disabled", false).val('');
});
</script>
<div id="catDPI" style="display:none;">
<select name="category" id="dpiCategory" class='category' style="width:210px;">
<option value="Playing Through Receiver">Playing Through Receiver</option>
<option value="arm bar">arm bar</option>
<option value="Grabbing">Grabbing</option>
</select>
</div>
<div id="catILF" style="display:none;">
<select name="category" id="ilfCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="moving">Moving</option>
<option value="illegal">Illegal</option>
<option value="standing">Standing</option>
</select>
</div>
<div id="catOPI" style="display:none;">
<select name="category" id="opiCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="restrict">Restrict</option>
<option value="holding">Holding</option>
</select>
</div>
The foulTypes .change function is working and the correct div is being displayed. The problem I am having is when I submit the form. If the #catDPI is visible the category menu selected value is submitted fine. If #catILF or #catOPI is visible the value for category is blank. I am not using the Ids of the category menus.
$("#submit-foul").submit(function(e){
e.preventDefault();
var category = $(".category").val();
var dataString = 'category=' + category;
if($("#submit-foul").valid()){
$.ajax({
url: 'myUrl',
type: 'POST',
success: function(data){
if(data == 1){
alert('Success');
};
}
});
}
return false;
});
I am not sure what I am doing wrong. Time to send it to the experts!! I appreciate all your help.
Try to change function "foulTypes" and add class in visible select.
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
$('#dpiCategory').addClass("visible");
} else {
$('#catDPI').hide();
$('#dpiCategory').removeClass("visible")
}
.... CONTINUE
In your ajax submit..
var category = $(".category.visible").val();
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
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>
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