Submit form use ajax in cakephp

PHP CAKE PHP JAVASCRIPT

Posted on 2014-11-26 02:32:52


Controller:

var $components = array('RequestHandler');
 
function index() 
{	
    if(!empty($this->data))
    {
	$this->set('msg',$this->data['Article']['name']);
    }
 
    if($this->RequestHandler->isAjax())        
    {           
	$this->viewPath = 'elements';
	$this->render('mydiv');                    
    }       
}

View:

<div id="LoadingDiv" style="display: none;">
    <?php echo $html->image('ajax-loader.gif'); ?>             
</div>
<?php if(!empty($ajax)) echo $javascript->link('prototype');?>
<div id = "mydiv">
    <?php echo $this->element('mydiv') ?>
</div>

Element mydiv.ctp:

<?php if(isset($msg)) echo $msg;?>
 
<?php echo date('I:s') ?>
 
<?php echo $ajax->form(array('type' => 'post','options' => array('model'=>'Article','indicator'=>'LoadingDiv','update'=>'mydiv','url' => array('controller' => 'articles','action' => 'index'))));?>
 
Your name: <?php echo $form->input('name',array('label'=>'')) ?>
 
<?php echo $form->end('OK') ?>