Reputation: 261
I am working in CakePHP to pass data via ajax. The data enter into the database successfully. This means the data pases from the view to the controller fine. However, it never populated the #target div. I have tried to even manually set the $data in the controller, but it still did not work. Here is my controller
<?php
App::uses('AppController', 'Controller');
class GenreselectsController extends AppController {
public $components = array('RequestHandler');
public $helpers = array('Js' => array('Jquery'));
// single search function
public function index() {
$this->loadModel('Notification');
if( $this->request->is('ajax') ) {
$this->autoRender = false;
$this->Notification->create();
$this->Notification->save($this->request->data);
$data = $this->request->data['Notification']['message'];
}
}
}
Here is my ajax
<script>
$(document).ready(function() {
$('#genresearch').change(function() {
var selectedValue = $('#genresearch').val();
var targeturl = '/genreselects/index/';
$.ajax({
dataType: "html",
type: "POST",
url: targeturl,
async : true,
data:{message:selectedValue},
evalScripts: true,
complete: function(data) {
//alert("hi neal"+selectedValue);
$("#target").append(data);
}
});
});
});
</script>
Here is my view div
<div id="target"></div>
What iam i doing wrong
Upvotes: 2
Views: 730
Reputation: 207
You have neither echoed/printed the data nor set it to any view(since you have set $this->autoRender = FALSE;), that is why you are not getting any data being set to the target .
It should be like:
1) If setting AutoRender to FALSE;
<?php
App::uses('AppController', 'Controller');
class GenreselectsController extends AppController {
public $components = array('RequestHandler');
public $helpers = array('Js' => array('Jquery'));
// single search function
public function index() {
$this->loadModel('Notification');
if( $this->request->is('ajax') ) {
$this->autoRender = false;
$this->Notification->create();
$this->Notification->save($this->request->data);
$data = $this->request->data['Notification']['message'];
echo $data;//echo data so that it is received by your ajax call
exit();
}
}
}
2) Setting content to view and rendering that as response in the target area:
<?php
App::uses('AppController', 'Controller');
class GenreselectsController extends AppController {
public $components = array('RequestHandler');
public $helpers = array('Js' => array('Jquery'));
// single search function
public function index() {
$this->loadModel('Notification');
if( $this->request->is('ajax') ) {
$this->Notification->create();
$this->Notification->save($this->request->data);
$data = $this->request->data['Notification']['message'];
$this->set(compact('data'));//here whole of the view with html will be returned as the response
}
}
}
Upvotes: 1
Reputation: 721
You have set $this->autoRender = false; so no view will be rendered use this instead
$this->render('sometemplatefile', 'ajax');
this wil use the ajax layout
add this line to index()
$this->set('message', $data);
create sometemplatefile.ctp and put this code in
<?php echo $message?>
Upvotes: 0
Reputation: 261
Got it. I had complete as the ajax function instead of success
<script>
$(document).ready(function() {
$('#genresearch').change(function() {
var selectedValue = $('#genresearch').val();
var targeturl = '/genreselects/index/';
$.ajax({
dataType: "html",
type: "POST",
url: targeturl,
async : true,
data:{message:selectedValue},
evalScripts: true,
success: function(data) {
//alert("hi neal"+selectedValue);
$("#target").append(data);
}
});
});
});
</script>
Upvotes: 0
Reputation: 5001
Somehow your action has to echo something or your Ajax call fetches a blank page, which means no data. There are many ways to do that.
The most classical is using a view:
GenreselectsController:
public function index() {
$this->loadModel('Notification');
if( $this->request->is('ajax') ) {
//$this->autoRender = false;
$this->Notification->create();
if($this->Notification->save($this->request->data)){
$message = $this->request->data['Notification']['message'];
}
else{
$message = 'something bad happened';
}
$this->set(compact('message'));
}
}
index.ctp
echo $message;
But you could for instance also return a JSON response:
GenreselectsController:
public function index() {
$this->loadModel('Notification');
if( $this->request->is('ajax') ) {
$this->autoRender = false;
$result = array();
$this->Notification->create();
if($this->Notification->save($this->request->data)){
$result['success'] = $this->request->data['Notification']['message'];
}
else{
$result['error'] = 'something bad happened';
}
$this->response->type('json');
$this->response->body(json_encode($result));
}
}
Or you could also use JSON or XML views
Upvotes: 0