CakePHP 1.3: How to Perform an Action Without Jumping to Its Presentation

I have a page using the search action, where the user can specify some parameters for calling data rows. This process takes a few seconds because quite a lot of logic is required. Based on the returned data, the user can add a data row to another table using the add_data action for further use.

Please note that the data of the search result is stored in a table so that I can easily get the row if the user decides to save it (the search result is a compilation of data from several databases). After the add_data action add_data I want to be able to save the search view with the search result, and not let the browser go to the add_data and return to the referrer, where the search result will not be.

Now I know that I can check this table, where I save the search result and simply load it into the search view, but I am interested in knowing whether it is possible to run another action, keeping the current action view without changes in the database. As far as I know, no $this->redirect(...) works because the URL of the search result page does not contain any arguments (e.g. mycontroller/search ).

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

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

Is there any way to do this? It is preferable to use minimal Javascript.

+4
source share
2 answers

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

Then enable the ajax helper in the controller, which will display your view:

 $this->helpers('Ajax'); 

You should be able to display your link like this and it will not direct the user to 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 submit data to this action, you might want to view the placement of the form instead of a simple link. Ajax Helper also provides the ability to submit a form through 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; ?> 
+2
source

I believe that you are 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!

0
source

All Articles