Im using cakePHP 2.2.1
I have a GoalsController that has an add action in it. This action saves posted data to Goals table.
Inside the add.ctp im using jQuery ajax method to post the form data to above said add action.
What i want is,
- ajax method posts form data to goals/add
- goals/add action saves data to goals table
- Obtain the id of newly inserted row using $this->Goal->id
- Return this id back to the onSuccess function of ajax method
Steps 1,2,3 are working fine. But i dont know how to implement step4.
I know php is server side and js is client side and all. Is there anyway i can achieve this?
Be sure to read the book entry on Json views and implement something like this:
Then you need to make your ajax post to
/goals/add.json, Cake will then recognise the.jsonextension in combination with the$this->set('_serialize')and return a json string along the lines of{ id: 1 }.Then in your
$.ajaxfunction, have a success call like this: