Tutorial - CakePHP Ajax "Quick Save" with jQuery
Tags: CakePHP, jQuery | Written on 15/7/08

When you are in an administration panel, sometimes you want a "quick save" feature that allows you to save without leaving the page. Here is how to accomplish this with CakePHP and jQuery.
To start, download jQuery and the jQuery Form Plugin JavaScript. Include them in your view with the JavaScript helper:
PHP:
Include the RequestHandler in your controller detect an Ajax save attempt. Also include the JavaScript helper if you haven't already.
PHP:
Next we want to override our save function with the ajax quick save. Put this right before your $this->Model->save($this->data) in your save action.
PHP:
if ($this->RequestHandler->isAjax()) { if ($this->Article->save($this->data)) { } Configure::write('debug', 0); $this->autoRender = false; }
This detects if the request is ajax, then saves the data. Then it sends back a simple, "success" message to let you know things went fine. It also writes debug to 0 and doesn't render anything, then exits.
Lastly, lets create and include a JavaScript file that performs the quick save.
JavaScript:
jQuery(function($){ $('<input type="button" value="Quick Save" />') .click(function(){ $(this).parents("form:first").ajaxSubmit({ success: function(responseText, responseCode) { $('#ajax-save-message').hide().html(responseText).fadeIn(); setTimeout(function(){ $('#ajax-save-message').fadeOut(); }, 5000); } }); return false; }) .appendTo('form div.submit'); });
This adds a button called, "Quick Save" to each form on the page where a div with class="submit" exists (you may want to switch this to the id of the form you want to add quick save to). Then It also attaches a click event to the button that submits the form via the jQuery Form Plugin.
In a few simple steps, we've created a quick save feature that saves your data whenever you want without leaving the page.
Comments
But, for work, i need to create a div with id ajax-save-message in my page, for print the success message, and i comment the Configure::write('debug', 0); and set autoRender to TRUE.
With this, the message are show.
Hello,
There seems to be a problem if we use the Security component.
Indeed, the use of this component, the hidden fields (hidden type) are not properly transmitted vision controller, and instead of saving it creates a new record in the table.
With the Security component, the id field of the page (or section) is transmitted like this if we use $ form-> hidden ( 'Page.id'):
data [_Page] [id]
Note the underscore before Page.
To solve the problem temporarily, you must write the hard-hidden field, such as:
<input type="hidden" name="data['Page']['id']" value="the_value" />
So, as much as I'd love to use this method, it seems that until the 411 error code that gets returned is fixed either by CakePHP or by allowing us to send a Content-Length header through the JQuery request, we may be forced to use some other methodology.
Unfortunately, I don't have a solution quite yet, but I've heard that .ajax() can work if you include a blank data{} variable. We'll see.
I hope this helps someone else looking for a solution to this problem.