Skip to content

Instantly share code, notes, and snippets.

@wenbert
Created November 5, 2012 00:51
Show Gist options
  • Save wenbert/4014610 to your computer and use it in GitHub Desktop.
Save wenbert/4014610 to your computer and use it in GitHub Desktop.
## the Controller
<?php
class FormfieldsController extends AppController {
public $helpers = array('Html', 'Form', 'Formfield');
public $components = array('RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
$this->Security->csrfExpires = '+1 hour';
if($this->Auth->user('role') === 'admin' || $this->Auth->user('role') === 'user') {
$this->Auth->allow('*');
}
$this->Security->unlockedActions = array('addrow');
}
/**
* handles AJAX requests to save a formrow
* return JSON
*
*/
public function addrow() {
$result = array();
if($this->request->is('ajax')) {
$result['status'] = 'success';
$result['message'] = __('Row added');
}
$this->set('result', $result);
}
}
## the view part with the AJAX
$.ajax({
'type': 'POST',
'cache': false,
'dataType': 'json',
'url': appUrl+'/formfields/addrow.json',
'success': function(data) {
console.log('success');
},
'error': function(jqXHR, textStatus, errorThrown) {
},
'complete': function(data) {
console.log('complete.');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment