how to call controller specific function from view



18.5k

Use AJAX, been his url like controller/action



16.0k


16.0k

How to return its response and show on a view



16.0k
edited Sep '14

I am not able to get post data in controller action

$.ajax({
type: "POST",
url: url_var,
data : Data_var,
success: function(msg){
if (msg.indexOf("Success") >= 0){
$(".price-box").html('<em>Successfully Added record.</em>').show();
}
else{
$("#error_result").html('Message:<em> '+msg+'</em>').show();
}
}
});
 function get_tech_priceAction(){
        echo 'in';
        echo '<pre>'; print_r($this->request->getPost()); echo '</pre>';exit;
   }



18.5k
edited Sep '14

Create a new Response object, do a $response->setJsonContent(array("response" => something)), then return $response.

For the AJAX (Jquery):

$.ajax(
        {
            url: '/controller/action',
            type: 'POST',
            async: false,
            dataType: 'json',
            data: {...}
        }).done(function (data)
        {
            console.log(data["response"]);
        });

In the controller's action, make a IF that checks if the request is a POST (for security).