0

I'm trying to save data from jquery but it doesn't work so I need your help! I'm using CakePHP 2.3. So this is my jQuery

$.ajax({
    url: 'http://localhost/test/reservations/save',
    type: 'POST',
    data: {reservation_time_from : calEvent.start, reservation_time_to: calEvent.end, user_id : "1", laboratory_id : "1"},
    success: function(data) {
        alert("saved")
    }
}); 

Controller

 public function save() {
    if ($this->data != null) {
        $this->Reservation->save($this->request->data);
    }
    $this->autoRender = false;
}

Maybe it doesn't work because of date format in jQuery (Mon Aug 26 2013 11:00:00 GMT+0200)?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
miha
  • 25
  • 4
  • Column name date should be DATETIME, check it what u have set,and before saving the data check the data is coming fine, and after save put false, $this->Reservation->save($this->request->data,false); – jsduniya Aug 26 '13 at 12:02

2 Answers2

1

Your data is not correctly formatted for passing it to Model::save(). Please refer to the documentation:

http://book.cakephp.org/2.0/en/models/saving-your-data.html

It should be in the following format:

Array
(
    [ModelName] => Array
    (
        [fieldname1] => 'value'
        [fieldname2] => 'value'
    )
)

So the object passed to the AJAX calls data property should look like this:

{ModelName: {fieldname1: 1, fieldname2: 2}}

In your case that would be:

{Reservation: {reservation_time_from: calEvent.start, reservation_time_to: calEvent.end, user_id: 1, laboratory_id: 1}}

Checking for a POST request instead of data being null might also be a good idea, ie:

if($this->request->is('post')) {
    $this->Reservation->save($this->request->data);
}

Also check if you are using the Security Component which may blackhole the request.

And last but not least, check for the date format you've already mentioned, in case validation is involved this might be a problem too, at least it's a problem in case the table column expects a different format. So, format the date properly if necessary, see Convert JS date time to MySQL datetime or http://arshaw.com/fullcalendar/docs/utilities/formatDate/ in case you are using FullCalender (just guessing by your event/property names).

Community
  • 1
  • 1
ndm
  • 59,784
  • 9
  • 71
  • 110
  • Thank you for help. Yes it is validation of datetime. If i comment validation for fields reservation_time_from and reservation_time_to it saves data to database but with date 0000-00-00 00:00:00 – miha Aug 25 '13 at 12:19
  • So you have to format the date properly, I've added two links to some examples. – ndm Aug 25 '13 at 12:33
0
data: {reservation_time_from : calEvent.start, reservation_time_to: calEvent.end, user_id : "1", laboratory_id : "1"},

is not valid json.

You need double quotes on your key to be considered valid.

If reservation_time_from is a variable, then you should create an object literal and add it in the following way:

myDataObject = {};
myDataObject[reservation_time_from] = calEvent.Start;

Once you have built your json object, you can then pass it in as the value for the data key.

carter
  • 5,074
  • 4
  • 31
  • 40
  • thank you for your response but still not working I m assuming there is problem with date format – miha Aug 24 '13 at 20:54
  • post more code. also, what are you getting back in response? console.log(data) so you can see what's comeing back. error: function(error){} as well so you can see if there are errors. How are you converting $_POST to data on server? these would help. :) – carter Aug 24 '13 at 20:56
  • @carter The `data` property doesn't expect a JSON string, it expects a plain string (a query string in the default configuration), or an key/value object or array. – ndm Aug 24 '13 at 23:38