Hi i have three dropdown, The first one populating value from database, Second and third will get their values by ajax call.
And the problem is at first loading of the page when i'm selecting the first dropdown the second dropdown change from please select and showing its value but third one is not showing and stay on "please select..." UNLESS i'm changing the value of second dropdown to anyother value and then values of third dropdown will appearing.
here is javascript:
<script type="text/javascript">
jQuery(function($){
$(document).ready(function() {
var Floor_id = 'Floor';
var Building_id = 'Building';
var Unit_id = 'Unit';
var initial_Floor_html = '<option value="">Please select...</option>';
var initial_Unit_html = '<option value="">Please select...</option>';
$('#'+Floor_id).html(initial_Floor_html);
$('#'+Unit_id).html(initial_Unit_html);
$('#'+Building_id).change(function(e) {
var selectfvalue = $(this).val();
//Display 'loading' status in the target select list
$('#'+Floor_id).html('<option value="">Loading...</option>');
if (selectfvalue == "") {
$('#'+Floor_id).html(initial_Floor_html);
} else {
//Make AJAX request, using the selected value as the GET
$.ajax({
url: 'index.php',
data:'option=com_parkingreservation&task=getFloorsHTML&fvalue='+selectfvalue,
success: function(output) {
//alert(output);
$('#'+Floor_id).html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}});
}
});
$('#'+Floor_id).change(function(e) {
var selectuvalue = $(this).val();
//Display 'loading' status in the target select list
$('#'+Unit_id).html('<option value="">Loading...</option>');
if (selectuvalue == "") {
$('#'+Unit_id).html(initial_Unit_html);
} else {
//Make AJAX request, using the selected value as the GET
$.ajax({
url: 'index.php',
data:'option=com_parkingreservation&task=getUnitsHTML&uvalue='+selectuvalue,
success: function(output) {
//alert(output);
$('#'+Unit_id).html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}});
}
});
});
});
</script>
and for ajax:
}
public function getFloorsHTML() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$myfloors = JRequest::getVar ('fvalue');
$query->select($db->quoteName(array('id', 'floor_name')));
$query->from($db->quoteName('#__pk_floors'));
$query->where($db->quoteName('building')." = ".$db->quote($myfloors));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo '<option value="' . $result->id . '" > ' . $result->floor_name.'</option>';
}
exit;
}
public function getUnitsHTML() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$myunits = JRequest::getVar ('uvalue');
$query->select($db->quoteName(array('id', 'unit_number')));
$query->from($db->quoteName('#__pk_units'));
$query->where($db->quoteName('floor')." = ".$db->quote($myunits));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo '<option value="' . $result->id . '" > ' . $result->unit_number.'</option>';
}
exit;
}
JRequest
as it's deprecated in Joomla 3.x. You should be using JInput. As for the actual Ajax and trying to populate the 3rd dropdown, are you getting any browser console log errors?