Tag Archives: ajax

Cakephp3: sending json response message to view

23666Method1:

$this->response->body(json_encode($msg));
return $this->response;

Method2:

$this->set('msg', $msg);
$this->set('_serialize', ['msg']);

//controller action

public function test(){		
		
		$msg="this is a call";
		
                //method 1 (chose a method)
		$this->response->body(json_encode($msg));
		return $this->response;	
		
		//method 2
		$this->set('msg', $msg);
		$this->set('_serialize', ['msg']);		
		
}

//javascript function call in view
function delete_all(){
	jQuery.ajax({
			type:'POST',
			async: true,
			cache: false,
			url:'mycontroller/test.json',
			success: function(response){					
				//success
				console.log(response);  				
			},
			error: function(response){					
				console.log(response);
			}
		});
}

 

Sample AJAX Call and Response

23666This is just a quick example of how to send a reply from your controller to n AJAX request.

//you controller

public function testajax(){
  $this->set('text', 'its a SUCCESS');
  $this->set('_serialize', ['text']);
}

//javascript function

<script>	
	function test(){
		$.ajax({
			type:"POST",
			url:"http://localhost/[YOURCAKEAPPNAME]/[CONTROLLERNAME]/testajax.json",
			async:true,
			success: function(response){
				//console.log('success');
				console.log(response.text);
			},
			error: function (response) {
				console.log('error');
				console.log(response.text);
			}
		});			
    }
</script>

A link to call the function;

<li><?= $this->Html->link(__('updateResult'), 'javascript:test()') ?></li>

 

Send Form data with ajax to cakephp controller

23666The following example shows how to submit a form with AJAX to a Cakephp controller;

Jquery Javascript Funtion;

	/*
	sends the form to the controller, ensure form fields names match up 
	with escpected values.
	*/
	function testajaxaddv3(){		
		jQuery.ajax({
			type:'POST',
			async: true,
			cache: false,
			url: 'http://localhost/mycakeapp/messages/ajaxadd',
			success: function(response) {					
				//success
				console.log(response);                
			},
			error: function(response) {					
				console.log(response);
			},
			data:jQuery('form').serialize()
		});
	}

CakePHP 3 Form:

<?= $this->Form->create(null) ?>
<fieldset>
<legend><?= __('Add Quizzes Answer') ?></legend>
<?php
echo $this->Form->input('message');
?>
</fieldset>

<?= $this->Form->end() ?>
</div>

<?php
echo $this->Form->button('save',['onclick'=>'testajaxaddv3()']);
?>

 

Cakephp3 Ajax data submission example

23666How to create a javascript function to send a AJAX request to your cakePHP controller action;

 

 

function testajaxadd(question_id,answerindex){ 

var mydata=new Object();
mydata.question_id=question_id;
mydata.answerindex=answerindex;

jQuery.ajax({
type:'POST',
async: true,
cache: false,
data: mydata,
url: 'http://localhost/myquiz/questions-answers/ajaxadd',
success: function(response) { 
//success
console.log(response); 
},
error: function(response) { 
console.log(response);
}
});
}

Cakephp controller action;

public function ajaxadd(){

$questionsAnswer = $this->QuestionsAnswers->newEntity(); 
if ($this->request->is('ajax')) {

$questionsAnswer = $this->QuestionsAnswers->patchEntity($questionsAnswer, $this->request->data);

// Added this line which makes the record belong to the logged in user
$questionsAnswer->user_id = $this->Auth->user('id');

if ($this->QuestionsAnswers->save($questionsAnswer)) {

$status['msg']="Saved Record"; 

} else {
$status['msg']="Error Saving Record"; 
}

//return the message to js function
$this->response->body(json_encode($status));
return $this->response; 
}
}