Reputation: 54979
I am trying to change a drop down based on a previously selected drop down. I am able to write the function in jQuery.
Since i am not able to use the URL path across various installation based on the server as i have to hard core the URL into the script.
<script type="text/javascript">
function getCategory(){
var industryId = $("#CompanyCompanyIndustryId").val();
$.getJSON('http://localhost/gitgrow/users/fetch/'+industryId, function(data) {
var options = '';
$.each(data, function(key, value) {
options += '<option value="' + key + '">' + value + '</option>';
});
$("#CompanyCompanyCategoryId").html(options);
})
}
</script>
How to achieve the same using CakePHP and JS component ?
Upvotes: 0
Views: 1472
Reputation: 29141
You can write your javascript in the page, and use $this->Html->url
to build your URL value. Just write it in $this->Html->scriptBlock();
and add 'inline'=>false
to make it write in the head instead of at that point of the document:
$this->Html->scriptBlock("
function getCategory(){
var industryId = $(\"#CompanyCompanyIndustryId\").val();
$.getJSON('" . $this->Html->url(array('controller'=>'users', 'action'=>'fetch', $industryId)) . ", function(data) {
var options = '';
$.each(data, function(key, value) {
options += '<option value=\"' + key + '\">' + value + '</option>';
});
$(\"#CompanyCompanyCategoryId\").html(options);
})
}
", array('inline'=>false));
Now you have javascript that's generated in the <head>
(or anywhere else you'd like it to be), and are using CakePHP to build the URL.
Upvotes: 1