We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

how to call controller specific function from view



33.8k

Use AJAX, been his url like controller/action



27.6k


27.6k

How to return its response and show on a view



27.6k
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;
   }


33.8k
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).