0

I have the following issue: in cakaphp does not advice anymore the using of jshelper,insted of normal javascript.

    <?php
$this->Js->get('#PostCategoryId')->event('change', 
$this->Js->request(array(
'controller'=>'subcategories',
'action'=>'getByCategory'
), array(
'update'=>'#PostSubcategoryId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>

I don't know how to execute cake controller in js(without using jshelper):

$this->Js->request(array(
'controller'=>'subcategories',

Normally in core php, i would do like this:

$("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){//select_type.php is the "controller"
    $("select#type").html(data);
    });
});

But how to use controller in javascript(jquery)?I am not sure but will it works if i do like this:

$.post("posts/subcategories/getByCategory", {id:id}, function(data){
3
  • Just give it a try it will works. It has nothing to do with cakephp its all jquery. try the console for url browse it on you browser directly, you will know. Commented Mar 31, 2015 at 5:03
  • yes it will work just dont forget to add root path in your url, which will be var webroot='<?php echo $this->webroot?>'; and than $.post(webroot+"YOUR CONTROLLER URL",... Commented Mar 31, 2015 at 6:28
  • i have a very few experience now with cakephp,lot of things are unclear. Thats why i need to ask a bit.Thank you so much your help Commented Mar 31, 2015 at 9:24

1 Answer 1

1

You could get the url like this using htmlhelper then it was would be a normal jquery call.

var url = <?php
    echo $this->Html->url(array(
    "controller" => "posts",
    "action" => "view"
));

?>;

http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.