Note: Ignore all comments
SCRIPT
<script type="text/javascript">var myBaseUrl = '<?php echo $this->base; ?>';</script> // finds the base URL
<script type="text/javascript">
$(document).ready(function(){
$('#DealTypemasterId').change(function(){ // when dropdown value gets changed function executes
$.ajax({
type : "POST",
url : myBaseUrl + ('/deals/populateSubcat') + '?q=' + $("#DealTypemasterId option:selected").val(), //pass query string to server
success : function(opt){
document.getElementById('DealSubcatId').innerHTML = opt; // assign the output to another dropdown
}
})
});
});
</script>
HTML
<?php echo $this->Form->create('Deal');?>
<?php echo $this->Form->input('typemaster_id', $typemasters);?>
<?php echo $this->Form->input('subcat_id', $subcats);?>
<?php echo $this->Form->input('merchantmaster_id', $merchantmasters);?><br />
<?php echo $this->Form->end(__('Submit', true));?>
PHP
Controller:
//fetch all data from database
function populateSubcat()
{
$location = $this->Subcat->find('list', array('fields' => array('name')));
$this->set('locations', $location);
$this->layout = 'ajax';
}
View:
//print the values set in controller in the format of javascript array, which is needed for autocomplete jquery.
<?php
$locat = implode(',', $locations);
echo $locat;
?>
exelent