laketuna
laketuna

Reputation: 4090

CakePHP 1.3: how to execute an action without going to its view

I have a page via the action search where a user can specify some options to bring up rows of data. This process take a few seconds because there is quite a bit of back-end logic required. On the returned data, the user has the ability to add a row of data to another table via the action add_data for future reference.

Note that the search result data is saved in a table in order for me to have the row readily available if the user decides to save it (the search result is a compilation of data from multiple databases). After the action add_data, I want to be able to maintain the search view with the search result instead of letting the browser go to the add_data view and go back to the referrer, where there will be no search result.

Now, I know I can check for this table where I save the search result and just have that loaded on the search view, but I'm interested in knowing if it's possible to run another action while keeping the current action view intact without consulting the database. As far as I know, no kind of $this->redirect(...) works because the search result page's URL lacks any arguments (e.g. mycontroller/search).

I added a button that adds the user-desired data using the following code:

echo $this->Html->link('+', array('action' => 'add_data', $reference_id));    // + button is created

Is there a way to do this? Preferably using minimal Javascript.

Upvotes: 4

Views: 2106

Answers (2)

psparrow
psparrow

Reputation: 9948

Follow the steps here to add ajax support to your cake project.

Then, enable the ajax helper in the controller that renders your search view:

$this->helpers('Ajax');

You should be able to render your link like this and it will not direct the user away from the search page:

<?php echo $this->Ajax->link(
  '+', 
  array('action' => 'add_data', $reference_id),
  array('update' => 'my-div')
); 
?>
<div id='my-div'></div>

Since you want the user to be able to send data to that action, you may want to look into posting a form instead of just a link. The Ajax helper also provides that ability to submit a form via Ajax:

echo $form->create();
echo $form->input('field1');
echo $form->input('field2');
echo $ajax->submit('Submit', array(
   'url'=> array('controller'=>'mycontroller', 'action'=>'add_data'),
   'update' => 'my-div'

));
echo $form->end();

In your controller....

function add_data() {
  // ... do something
  $this->layout = 'ajax';
  $this->set('message', 'Your message here!');
}

In the add_data view....

<?php echo $message; ?>

Upvotes: 2

Andrew Senner
Andrew Senner

Reputation: 2509

I believe you're looking for the View::render method.

<?php

...
function addData(...) {
    ...
    ...

    $this->render('search'); // Render the search view instead of the "add_data" view.
}
...

?>

or maybe

<?php

...
function search(...) {
    ...
    ...

    if (<criteria)
        $this->addData(...); // Move to another action while keeping the current view in tact.
}
...
function addData(...) {
    if (<criteriaForSearchView>)
        $this->render('search');
    // Else render the "add_data" view.
}
...

?>

Let me know if this helps! Thanks!

Upvotes: 0

Related Questions