Breaking News
You are here : Home / Web development / CakePHP / Jquery Post Request to CakePHP Controller’s Action
Jquery Post request To cakePhp Controller

Jquery Post Request to CakePHP Controller’s Action

Jquery POST Request to cakePHP  Controller action:

Hello Guys ! Today I came up with Sending Jquery Post requests to cakePHP Controller’s action . I found that this post might be helpful to people as there is very less cakePHP documentation and help. I was working on a project and the Ajax post was the requirement . This Could be helpful for you if you wana implement Suggestion search or something like that in cakePHP.

I have seen that CakePHP Ajax Helper is not that much useful in some cases ,So we always like to Send Jquery Ajax Post requests to CakePHP conroller’s action.

So let’s see the Script below for Jquery Ajax Post request to cakephp action:

Jquery Ajax Post request to cakePHP controller action:

See This Script ! What this will do is to make an Jquery Ajax Post Request to cakePHP Action .In this case the Controller is books and actions is add.

[jbox color=”green” title=”Jquery Ajax Post Request”]

var inputstring=$(“#list”).val();
$.post(“<?php echo $this->Html->url(array(‘controller’=>’books’,’action’=>’add’),true) ?>”, //Specify Action Url
{“data[Book][name]”: “”+inputstring+””}, //Send post data here
function(data){     //Operation if data received
if(data.length >0) {
$(‘#resultbox’).fadeIn();
$(‘#resultbox’).html(data); //load data in resultbox
}
});
[/jbox]

So this Script will send Ajax post request to the Cakephp action you will specify.You can see that we send data already in format that cakePHP controller expect from us(data[Book][name]) and will store is data without any issue.So Script will work fine But when you need a return data from the Jquery Ajax Post request made to cakePHP controller action then cake php action will render and send the whole layout along with that data,As this is a Ajax post request we just require data not the  extra layout ,So what we gonna do is configure our controller action and tell it if the Request is post then do not render any layout or render an empty layout .

Controller Setup for Jquery Ajax Post Request :

Add these Lines in your Controller Action.In this case These lines are added in Add() action of Books Controller,As above we send Jquery Post request to books Controller’s Add action

[jbox color=”green” title=”Controller Action Setup Jquery Ajax Post Request”]

if($this->RequestHandler->isAjax()){
$this->layout=’empty’;//render an empty layout
$this->render(‘/elements/ajaxreturn’); // render view created by us specially for Ajax data

}
[/jbox]

Now The code “$this->RequestHandler->isAjax()” might not work for you if dont have included “RequestHandler” Component in your AppController or Simly your controller ,,whatever is name of your controller .

So Include this component as well:

[jbox color=”green” title=”Controller Action Setup Jquery Ajax Post Request”]

var $components = array(‘RequestHandler’);

[/jbox]

That all ! now your code will run smooth .Comment if any problem.and very very thanks for visiting Designaeon.com and reading our post.

Please Like and Share if You found this Useful.

[separator/]

Tags : , ,

1 Reply

Leave a Reply

Want to join the discussion ? Feel Free to contribute !