We are using an old version of CakePHP for our App API and backend. I have created a new view page and am trying to call a controller to do work and return something to the view.
I have followed the post here: CakePHP Sending data from view to controller via ajax
and added in some code into the controller to show that it is indeed getting there, which it is (I get the message and ID in the debug.log):
$parentID = $this->request->data['id'];
CakeLog::write(LOG_DEBUG, "Parent ID is ".$parentID);
But nothing gets back to my view. How can I start to debug this? I do not understand how the ajax_response gets back to my div #dataToshow?
Here are my files:
View (parent.ctp):
<script>
$(function() {
$('#btnCreatePDF').click(function() {
$.ajax({
type:"POST",
datatype:'json',
cache: false,
data: $("#parentSubmissionDetails").serialize(),
url:"/v2.0/api/parentSubmission2PDF",
update:"#dataToshow"
});
});
});
</script>
<div id="dataToshow"></div>
<form id="parentSubmissionDetails">
<input id="id" name="id" type="hidden" value="<?php echo $parent['ParentData']['id']; ?>">
</form>
<button id="btnCreatePDF">Do something with AJAX</button>
Controller function (parentSubmission2PDF):
public function parentSubmission2PDF()
{
if ($this->request->is('ajax')) {
// Treatment of the new data here
//get the parent submission id
$parentID = $this->request->data['id'];
CakeLog::write(LOG_DEBUG, "Parent ID is ".$parentID);
//set current date as content to show in view
$this->set('content', $parentID);
//render spacial view for ajax
$this->render('/Api/ajax_response', 'ajax');
}
}
Ajax Response View (ajax_response.ctp):
<?php echo $content; ?>