I'm trying to write this code to retrieve data from the first Select option to the second Select:
Fist Select:
<select name="league">
<option value="0">------------Please Select League-------</option>
<?php
$sql_league="select * from ".$prev."league";
$re_league=mysql_query($sql_league);
while($d_league=mysql_fetch_array($re_league))
{
?>
<option value="<?=$d_league['id']?>" <?php if($_SESSION['id']==$d_league['id']){?> selected="selected" <?php }?>><?=$d_league['title']?></option>
<?php
}
?>
</select>
Second Select:
<select name="team">
<option value="0">------------Please Select team-------</option>
<?php
$sql_team="select * from ".$prev."team where leagueID=".$d_league['id']."";
$re_team=mysql_query($sql_team);
while($d_team=mysql_fetch_array($re_team))
{
?>
<option value="<?=$d_team['id']?>" <?php if($_SESSION['id']==$d_team['id']){?> selected="selected" <?php }?>><?=$d_team['title']?></option>
<?php
}
?>
</select>
Second Select should depend on the selection from the first Select (If I chose League1, the Second select options should show teams assigned to that league).
any Ideas how to make this work?
try this
<select name="league" onChange="bindSecondSelect()">
<option value="0">select</option>
// your php code
</select>
<select name="team">
// your php code
</select>
<script type="text/javascript" src="jquery-1.7.js"></script>
<script type="text/javascript">
function bindSecondSelect()
{
if($('[name="league"]').val() != "0")
{
$('[name="team"]').find('option').css('display','none');
$('[name="team"]').find('option[value="'+$('[name="league"]').val()+'"]').css('display','block');
}
}
</script>
Send an ajax request to a function with the first select value. This function should get the details of that value and returns the details to the browser. Use that details and append it into the second select.
I have created a little fiddle for you http://jsfiddle.net/TqKG3/3/.
I have used some default values to demonstrate, you will have to use ajax call and return json.
For eg :
$("#opt_leg").change(function () {
$("#opt_mem").find('option').remove();
var url = "your_url.php?league_id=" + $(this).val();
$.get(url, function(data){
var list = jQuery.parseJSON(data);
$.each(list, function (i, item) {
$("#opt_mem").append("<option value=\"" + item.id + "\">" + item.name + "</option>");
});
});
});
Related
Need assistance
I need to have a select function where as
<select class="form-control">
<option value="4100">4100 : Mass Collections</option>
<option value="4200">4200 : Stole Fees</option>
<select>
now I don't want to include the code description after selecting an option, it should be code only appearing on the select box like this.
I have tried this Jquery script
$('select').change(function() {
var code= $('option:selected',this).text().split(' : ');
$('option:contains('+code[0]+')',this).val(code[0]);
})
but every time I do this checking in dropdown the option omitted the description entirely. How do I prevent this.
thanks in advance
You can follow the idea of this answer: add the description only when the element have focus:
$('select').on("focus", function(s) {
for (var o of this.options) {
o.textContent = o.getAttribute('value') + ': ' + o.dataset.descr;
}
});
$('select').on("blur", function(s) {
for (var o of this.options) {
o.textContent = o.getAttribute('value');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control">
<option value="4100" data-descr="Mass Collections">4100</option>
<option value="4200" data-descr="Stole Fees">4200</option>
<select>
This is the best that I could do.
$(document).ready(function (){
$('select').change(function(s) {
for (var option of this.options) {
option.label = option.text;
};
this.selectedOptions[0].label = this.selectedOptions[0].value;
});
});
Better to check it with more options,
<select class="form-control">
<option value="4100">4100 : Mass Collections</option>
<option value="4200">4200 : Stole Fees</option>
<option value="4300">4300 : Something Else</option>
<select>
I am not sure the title is explained perfectly.
I have 2 select tags and both select tags data are coming from the same table, in
The fist select tag - I am fetching data from the database which stores location name => This works perfectly and I have 3 locations name in the database.
<select name="location" class="form-label" id="location" required>
<option value="">Select Dojo Location</option>
<?php foreach ($location as $row): ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->location; ?></option>
<?php endforeach; ?>
</select>
Image for first select tag
The second select tag - I am fetching prices from different columns of the same table but it shows every price from different locations.
<select name="month" class="form-label" id="month" required>
<option value="">Select Fees Period</option>
<?php foreach ($location as $row1): ?>
<option value="<?php echo $row1->admission_fee_1; ?>">Monthly</option>
<option value="<?php echo $row1->admission_fee_1; ?>">Quarterly</option>
<option value="<?php echo $row1->admission_fee_6; ?>">Half Yearly</option>
<option value="<?php echo $row1->admission_fee_12; ?>">Annually</option>
<?php endforeach; ?>
<?php endif; ?>
Image for second select tag
Image for table data
What I am trying to do is, when user select location in 1 select tag, so it only shows the prices of that selected location from database in second select tag.
This is what I am getting in the result
But I do not succeed yet, any solution is helpful or is there any other way to do this or am I doing it wrong.
Thanks in advance.
You have to use JavaScript and onChange method. It was resolved manytimes, e.g. how to change a selections options based on another select option selected?
You need to make some ajax call.
On Selection of first location tag; you can make jQuery Ajax call and get prices of that selected location only. In Ajax response you can receive HTML of second select tag and replace with current one.
Let me know if you need further explanation.
You need fetch the data dinamically.
Something like this
$( "#location" ).change(function() {
// this.value is your ID
$.post('/endpoint/fetch-details/' + this.value)
.done(function(data) {
// load data into another SELECT
})
.fail(function() {
alert( "error" );
})
});
On Codeigniter you could have a controller method (/endpoint/fetch-details/) that returns a JSON object.
Successfully Done...
Ajax function =>
$(document).ready(function () {
$('#location').on('change', function () {
var location_id = $(this).val();
if (location_id == '') {
$('#fees').prop('disabled', true);
} else {
$('#fees').prop('disabled', false);
$.ajax({
url: "<?php echo $base_url;?>welcome/getFeePeriod",
type: 'POST',
data: {location: location_id},
dataType: 'json',
success: function (data) {
//alert("Ok");
$('#fees').html(data);
},
error: function () {
alert("Error Accured...");
}
});
}
});
});
Controller function =>
public function getFeePeriod()
{
$location_id = $this->input->post('location');
$FeesPeriod = $this->admin_model->getFeePeriod($location_id);
if (count($FeesPeriod)>0) {
$fee_select_box = '';
$fee_select_box .= '<option id="">Select Fee Period</option>';
foreach ($FeesPeriod as $fees) {
$fee_select_box .= '<option id="' . $fees->admission_fee_1 . '">Monthly</option>+
<option id="' . $fees->admission_fee_3 . '">Quarterly</option>+
<option id="' . $fees->admission_fee_6 . '">Half Yearly</option>+
<option id="' . $fees->admission_fee_12 . '">Yearly</option>';
}
echo json_encode($fee_select_box);
}
}
model function =>
function getFeePeriod($location_id){
$query = $this->db->get_where('location', array('id'=>$location_id));
return $query->result();
}
Thanks everyone for their response...
I am trying to create two dropdown lists with the condition, that the second one should show the list based on the value, selected in the first one.
1st is city.
2nd is district within the city.
Both lists are populated from the database.
Here is an example
<td>City:</td>
<td>
<?=form_dropdown('contact_city', $sel_city, set_value('contact_city'), "class='select2'"); ?>
</td>
<td>District:</td>
<td >
<?=form_dropdown('contact_district', $sel_contact_district, set_value('contact_district'),"class='select2'"); ?>
</td>
$sel_contact_district should be reloaded and list selection updated after city value is changed.
Which approach should I use?
Using ajax you could populate the second dropdown based on the value of the first. Would look something like that:
$("#cityDropdown").on('change', function(){
$.post('api/listdistrictsfromcity',
{ city_id : $(this).val() },
function(response){
var $district_dropdown = $("#districtsDropdown").empty();
$.each(response, function(i, item){
$district_dropdown.append("<option value='" + item.value + "'>"+ item.text +"</option>");
});
});
});
Select2 supports methods that allow programmatic control of the component.
// var $example = $(".js-example-programmatic").select2();
$('body').on('click', '.select2', function(e) {
//var select_name = $(this).attr("your_select2_name");
$(this).val("your_option").trigger("change");
});
You can also add options like this:
$('.select2').select2('data', {id: 100, a_key: 'Lorem Ipsum'});
3rd Edit:
As you mention in your question that you need to select the option on the select dropdown to depend on the first dropdown selected option.
Here is the Two select box which changes the option depending on the first select box.
$("#select1").change(function() {
if ($(this).data('options') === undefined) {
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<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>
Noob here trying to do something simple with Jquery. Basically I have a small select-option dropdown like this:
<select onchange="javascript:swapContent('con3');">
<option>selection 1</option>
<option>selection 2</option>
<option>selection 3</option>
</select>
Currently it is posted via ajax to a $php variable here:
$contentVar = $_POST['contentVar'];
javascript function is here:
<script language="JavaScript" type="text/javascript">
function swapContent(cv) {
$("#myDiv").html('<img src="loader.gif"/>').show();
var url = "myphpscript.php";
$.post(url, {contentVar: cv} ,function(data) {
$("#myDiv").html(data).show();
});
}
</script>
How can I instead post the value of the select-option dropdown?
I realize this is basic, but I have to start somewhere and cannot find a tutorial that shows me how to do what I need. Thank you for your help.
How can I instead post the value of the select-option dropdown?
If you want to send values yo tour script you should start by giving values to your option elements (and an id to your <select> element to be able to use it and retrieve the selected value):
<select onchange="javascript:swapContent('con3');" id="myselect">
<option value="value1">selection 1</option>
<option value="value2">selection 2</option>
<option value="value3">selection 3</option>
</select>
and then:
// get the currently selected value from the dropdown
var value = $('#myselect').val();
$.post(url, { contentVar: cv, selectedValue: value }, function(data) {
$("#myDiv").html(data).show();
});
If on the other hand you wanted to send the text of the selected option you could use this:
// get the currently selected value from the dropdown
var text = $('#myselect option:selected').text();
$.post(url, { contentVar: cv, selectedText: text }, function(data) {
$("#myDiv").html(data).show();
});
Now inside your PHP script you could fetch the value:
$_POST['selectedValue']
HTML:
Remove the inline javascript change event from the select.
<select>
<option>selection 1</option>
<option>selection 2</option>
<option>selection 3</option>
</select>
jQuery:
Bind a change event to your select. Inside this event, this.value with be the value of the select.
$(document).on("change", "select", function(){
$("#myDiv").html('<img src="loader.gif"/>').show();
var url = "myphpscript.php";
$.post(url, {contentVar: this.value} ,function(data) {
$("#myDiv").html(data).show();
});
});
var val = $('select option:selected').text();
Using jQuery's selector syntax, you first select the "select" control, and then the "option" child of the select control. You then look for the selected state. Finally, return the text.
Of course, as is, this code will select every "select" control on the page. It would be better to give your select control an id:
<select id="mySelect">
<option>Some option</option>
<option>Some other option</option>
</select>
Then the code becomes
var val = $('#mySelect option:selected').text()
You might want to consider an unobtrusive JavaScript approach and not have the on-change event in your html mark-up, instead attach an event handler as shown below, from the jQuery API site
<!DOCTYPE html>
<html>
<head>
<style>div { color:red; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<select name="sweets">
<option>Chocolate</option>
<option>Candy</option>
<option>Taffy</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
<div></div>
<script>
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
$("div").text(str);
})
.change();
</script>
</body>
</html>
I have this script and I want to get the value of drop down select list in the PHP and check the if (jws1 != "") then show 1 to 10 value in the second drop down box using of for loop...
JavaScript code:
<script language="javascript" type="text/javascript">
function test()
{
var e = document.getElementById("JWSections");
var Sections = e.options[e.selectedIndex].value;
alert(Sections);
}
</script>
HTML code:
<select name="JWSections" id="JWSections" onchange="test();">
<option value="">Select Sections</option>
<option value="jws1">Section 1.1</option>
<option value="jws2">Section 1.2</option>
<option value="jws3">Section 1.3</option>
<option value="jws4">Section 1.4</option>
</select>
I want to get the value like jws1, and check in the PHP.
For PHP to be able to do anything with your HTML form data, you'll have to submit it to PHP.
You can either submit the form itself or use AJAX to pass in the value of jws1 and build the options from the result. Jquery is pretty good with doing that kind of thing, all you gotta do is set your AJAX request's data type to HTML and assign the data from the "success" callback to your destination drop down list.
<select name="JWSections" id="JWSections" onchange="test(this);">
<option value="">Select Sections</option>
<option value="jws1">Section 1.1</option>
<option value="jws2">Section 1.2</option>
<option value="jws3">Section 1.3</option>
<option value="jws4">Section 1.4</option>
</select>
<select name="JWSections" id="secondDropDown"> </select>
<script language="javascript" type="text/javascript">
function test(dropdown)
{
var selectedjws = dropdown.options[dropdown.selectedIndex].value;
var secondDropDown = document.getElementById('secondDropDown');
$.post('path', { selectedjws : selectedjws }, function(key, value){
// Remove all options from second dropdown
for(var i = 0; i < secondDropDown.options.length; i++){
secondDropDown.remove(i);
}
// Add options by key value pair returned from server
secondDropDown.add(new Option(value, key))
});
}
</script>
jsfiddle: http://jsfiddle.net/KTq6y/1/