ANY good / simple Ajax examples in Cake 1.3 without using obsolete helpers?

I used AJAX a lot in PHP, but now I'm trying to learn CakePHP and could not ANY good / simple example of using AJAX with CakePHP 1.3. The only examples I could find were swap (not what I needed), or using outdated helpers, forcing you to include a script or prototype.

Are there any good / simple examples of using AJAX w / CakePHP 1.3? Or can someone here explain this?

All I want to do is have the user click on a link that extracts the contents of the php file and inserts it into a div. The contents of the php file will be modified based on some POST or GET variables sent with ajax call. Seems pretty simple :(

+4
source share
3 answers

This is the template I use in CakePHP 1.3.x. Usually this process:

  • Create alternative json-oriented layouts and view files
  • Detecting if an incoming AJAX request
  • Explicit display of alternative json layout / view instead of text / html

Be sure to include the RequestHandler and JsHelper component in the application controller.

In this controller:

function test() { if($this->RequestHandler->isAjax()) { $this->set('data', $this->data); // Explicit call to render an ajax response, using a layout and view made specifically for ajax $this->layout = 'json'; $this->render('ajax_test'); } // else render views/controllername/test.ctp like normal } 

Your application /views/layouts/json.ctp file:

 <?php header("Pragma: no-cache"); header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate"); header('Content-Type: text/x-json'); header("X-JSON: ".$content_for_layout); echo $content_for_layout; ?> 

In your file app / views / controllername / ajax_test.ctp should only be:

 <?php echo $js->object($status); ?> 

Now, on your page, which is actually going to make an ajax call, it might look something like this: with jQuery code:

 <div id='status'></div> <?php echo $form->create('Test', array('id'=>'testForm')), $form->input('message'), $form->end(); ?> <script type='text/javascript'> $('#testForm').submit(function(event) { event.preventDefault(); // interrupt form submission $.ajax({ type: "POST", url: "/controllername/test", data: $('#testForm').serialize(), success: function(data, textStatus, xmlHttpRequest) { $("#status").html(data.Test.message); }, error: function(jqXHR, textStatus, errorThrown) { alert("There was a problem processing the request: " + jqXHR); } }); }); </script> 
+3
source

You can check http://wakeusup.com/2011/06/kkajaxify-plugin-cakephp/ for a great ajax plugin

+1
source

I usually include the js file in the required view $this->Html->script('file', false); . Then in this file I make all the necessary AJAX stuff (I create the data that needs to be sent, send it to another page, return the data and finally show it in the desired div).

But keep in mind that the file sent by the data must be relative to the root of the application. This is the only way to make me work ( $.get('/users/view/5'); - in jQuery)

0
source

All Articles