data is not saving into database using cakephp 2.0

304 Views Asked by At

i am working on a Cakephp 2.x .. i am sending data from to my controller using Ajax Post but data did not saving into the database??

Form and Ajax

$('body').on('submit','.contact_form', function () {
var action = $(this).attr('action');
var postdata = $(this).serialize();

$.ajax({
 type: 'POST',
 url: action,
 data: postdata,
 dataType:"html",

 success: function(data, textStatus, xhr) {
 },

 error: function(xhr, textStatus, error) {
        alert(textStatus);
 }
 });
 return false;  

 });
 });

<?php echo $form->create('Listingbookmark',array('action'=>'add/','class'=>'contact_form'));?>
<input type="hidden" name="listingid" value="<?php echo $listingData['Listing']['id'] ?>">
<input type="hidden" name="userid"    value="<?php echo $user_id ?>">

<input type="submit" value="Bookmark">
<?php echo $form->end(); ?>

Model

class Listingbookmark extends AppModel
{
    var $name = 'Listingbookmark';
    var $useTable = 'listingbookmark';
    var $order = array('Listingbookmark.id asc');   
}

Controller Function

function add()
{

$this->request->data['Listingbookmark']['listingid'] = $_POST['listingid'];
$this->request->data['Listingbookmark']['userid'] = $_POST['userid']; = $_POST['listingid'];
$this->Listingbookmark->create();
$this->Listingbookmark->save($this->request->data);

}

when i try to echo both variables it is showing values but did not saving in database

echo $this->request->data['Listingbookmark']['listingid'] =   $_POST['listingid'];
echo $this->request->data['Listingbookmark']['userid'] = $_POST['userid'];
$this->Listingbookmark->create();
$this->Listingbookmark->save($this->request->data);
1

There are 1 best solutions below

2
Ravi Roshan On
$this->data['Listingbookmark']['userid'] = $_POST['listingid'];  ?????

why you are using saveAll method of cakephp to insert data into table if you needs to insert data in only one table. use cakephp's save() method instead of saveAll() method.