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

Execute action on Same Page

Hello, I have a controller with two actions index and send action.

I want to execute send action without leaving the page.

Thanks

edited Jun '16

What's stopping you from doing the following:

public function indexAction()
{
    $this->sendAction();
}

public function sendAction()
{

}

You can even make sendAction() into private method like private function send() to avoid executing it from url.

However i want to execute it when a button is clicked.

Then you have to use AJAX, no other option :)

Am I correct in believing you want the user to be at a url, for example: www.website.com/controllername/, and you want them to click a button, then have ControllerNameController::sendAction() execute?

Am I correct in believing you want the user to be at a url, for example: www.website.com/controllername/, and you want them to click a button, then have ControllerNameController::sendAction() execute?

Yes, but I want the application to execute it on the same page somehow.

Do you have data from teh first action that needs to be made available in the second action?

This sounds like a view / Interface need.

// i am using jquery here for teh simple get method. 

$('#yourbuttonId').click(function(){
    $.get('/controllerName/secondAction/',{data},function(response){
        // do stuff with response? 
    },'JSON');
});

public function secondAction()
{
    // here you can add some testing logic to make sure its not being visited directly. 
    if ($this->request->isAjax()){
        //do stuff
    }   
}