populating a select box with javascript - php

I currently have an auto populating select box depending on what option you select in the first select box. This is all working fine and does the job.
What i am trying to achieve is when the form is submitted it retains the values selected. I can get this working for the first initial select box, but when it comes to the second select box (the one loaded automatically) i cant seem to get it working.
The first select box id is ctlJob with the static information, and the second one has an id of ctlPerson.
Here is my HTML:
<form id="homesearch" action="view.php" method="get">
<label for="ctlJob"></label>
<select name="id" id="ctlJob" style="clear:both; float:left;">
<option value="0">Please Select...</option>
<option <?php if ($_GET['id'] == '1') { ?>selected="true" <?php }; ?>value="1">Regional General Manager</option>
<option <?php if ($_GET['id'] == '2') { ?>selected="true" <?php }; ?>value="2">General Manager</option>
<option <?php if ($_GET['id'] == '3') { ?>selected="true" <?php }; ?>value="3">Deputy General Manager</option>
<option <?php if ($_GET['id'] == '4') { ?>selected="true" <?php }; ?>value="4">Operations Manager</option>
<option <?php if ($_GET['id'] == '5') { ?>selected="true" <?php }; ?>value="5">Shift Manager</option>
<option <?php if ($_GET['id'] == '6') { ?>selected="true" <?php }; ?>value="6">First Line Manager</option>
<option <?php if ($_GET['id'] == '7') { ?>selected="true" <?php }; ?>value="7">Stock Controller</option>
<noscript>
<input type="submit" name="action" value="Load Individuals" />
</noscript>
<select name="person_id" id="ctlPerson" style="clear:both; float:left;">
</select>
<input id="submit" type="submit" name="action" value="search" style="margin-top:40px;" />
</form>
Here is my js:
<script type="text/javascript">
document.getElementById('id').value = "<?php echo $_GET['id'];?>";
document.getElementById('person_id').value = "<?php echo $_GET['person_id'];?>";
</script>
Any ideas on how i can get the secondry select box to stay populated with the option selected after the form has been submitted to itself?
Thanks, Dan
Sorry i missed this js out:
<script type="text/javascript" charset="utf-8">
$(function(){
$("select#ctlJob").change(function(){
$.getJSON("view.php",{id: $(this).val(), ajax: 'true'}, function(data){
var $persons = $("#ctlPerson").empty();
$.each(data, function() {
$persons.append("<option value=" + this.optionValue + ">" + this.optionDisplay + "</option>");
});
})
});
})
</script>

Whatever code you use to populate it (presumably a change event handler), just call it on page load. I do stuff like that with this kind of code:
(elem.onsomething = function() {
// some event-handling code
}).call(elem);

Related

Could not set the select option value using jquery and PHP

I have an issue. I need to set the select value as text in the select box using jQuery but unable to do it.
<select name="countryCode" class="chosen-select" id="countryCode" required>
<?php foreach ($countrydata as $key => $value) {
if ($value['con_code']=='IN') {
$select='selected';
}else{
$select='';
}
?>
<option data-text="<?php echo $value['country']; ?>" value="<?php echo $value['dial_code']; ?>" <?php echo $select; ?>><?php echo $value['country']; ?></option>
<?php } ?>
</select>
My script part is given below.
$('#countryCode').find(':selected').html($('#countryCode').find(':selected').attr('value')); // already changed onload
$('#countryCode').on('change mouseleave', function () {
$('#countryCode option').each(function () {
$(this).html($(this).attr('data-text'));
});
$('#countryCode option:selected').html($('#countryCode option:selected').attr('value'));
$(this).blur();
});
$('#countryCode').on('focus', function () {
$('#countryCode option').each(function () {
$(this).html($(this).attr('data-text'));
});
});
What I need is that when the user selects any text, it's value will be shown to the user. In my code it's not happening like this.
1st: a little optimization of your JS :
$('#countryCode option:selected').html($(this).val());// already changed onload
Now, how to select an option using JS :
$(function() {
var $sel = $('.chosen-select');
$('#exists').click(function() {
$sel.val('option3');
console.log($sel.val());
});
$('#disabled').click(function() {
$sel.val('option2');
console.log($sel.val());
});
$('#noExist').click(function() {
$sel.val('option5');
console.log($sel.val());
});
$sel.on('change', function() {
console.log($sel.val());
});
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<button id="exists">Select 3rd option</button>
<button id="disabled">Select disabled option</button>
<button id="noExist">Select inexistant option</button>
<select class="chosen-select">
<option>option1</option>
<option disabled>option2</option>
<option>option3</option>
<option selected>option4</option>
</select>
<button onclick="console.clear();">X</button>
As you can see, only existant and not disabled options can be selected.
should be something like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function(){
$('#countryCode').on('change', function(){
var val = $(this).val();
$(this).find("option").each(function(){
$(this).text($(this).attr("data-text"));
});
$(this).find(":selected").text(val);
}).trigger("change");
});
</script>
<select id="countryCode" class="chosen-select">
<option data-text="A Text" value="A value">A Text</option>
<option data-text="B Text" value="B value">B Text</option>
<option data-text="C Text" value="C value" selected>C Text</option>
</select>

jquery ajax submit only submitting first value

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();

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

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.

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>

Categories