This is my view ctp page:
<h1>Add Post</h1>
<?php echo $this->form->create(null,array('url'=>array('controller'=>'posts','action'=>'ajaxAdd'),'id'=>'saveForm'));
echo $this->form->input('ajaxtitle');
echo $this->form->input('ajaxbody',array('rows'=>'3'));
echo $this->form->end('Save Post');
?>
<script>
$(document).ready(function(){
$("#saveForm").submit(function(){
var formData = $(this).serialize();
var formUrl = $(this).attr('action');
$.ajax({
type:'POST',
url:formUrl,
data:formData,
success: function(data,textStatus,xhr){
alert(data);
}
});
return false;
});
});
</script>
This is my PostsController function
class PostsController extends AppController
{
public $name = 'Posts';
public $helpers = array('Html', 'Form', 'Session');
public $components = array('RequestHandler');
public function ajaxAdd()
{
$this->autoRender=false;
if($this->RequestHandler->isAjax()){
Configure::write('debug', 0);
}
if(!empty($this->data)){
$inputData = array();
$inputData['Post']['title'] = $this->data['Post']['ajaxtitle'];
$inputData['Post']['body'] = $this->data['Post']['ajaxbody'];
$data = $this->Post->findByTitle($inputData['Post']['title']);
$this->Post->create();
if(empty($data))
{
if($this->Post->save($inputData))
return "success";
}else
{
return "error";
}
}
}
}
With array as $inputData in save , whenever I click over the submit button nothing is being saved in the database.
But when I pass $this->data in save function, columns like id,created and modified are filled but the title and body column are left blank.
My posts database contains columns id,title,body,created,modified.