Ajax implementation on CakePHP @ 3

Recently i have started another CakePHP project. i found that the example in Ajax implementation on CakePHP @ 2 should be changed as follow.

ajax_controller.php

<?php
Class AjaxController extends AppController {
	var $name = 'Ajax';
	var $helpers = array('Form','Html','Javascript', 'Ajax');
	var $components = array('RequestHandler');

	function afterAjaxAction () {
		// get the $this->data to collect the form information
		// save the object to database
		// render the after_ajax_call.ctp
		$this->render('/elements/layout/after_ajax_call');
	}
}


 

view.ctp

<div id='ajaxDiv'>
	<!-- create the form -->
	<?php echo $form->create(<object to be saved>); ?>
	<?php echo $form->input(...); ?>
	<?php echo $form->input(...); ?>
	<?php echo $form->input(...); ?>
	<?php echo $ajax->submit('Submit', array('url' => array('controller'=>'ajax', 'action'=>'afterAjaxAction'), 'update'=>'ajaxDiv', 'before' => <OPTIONAL - Ajax callback option for javascript>)); ?>
	<?php echo $form->end(); ?>
</div>

 

Done =)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.