CakePHP Ajax Login

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
ChrisBull
Forum Commoner
Posts: 42
Joined: Fri Aug 20, 2010 7:43 am

CakePHP Ajax Login

Post by ChrisBull »

Hi, firstly this is a long question but I hope somone can help me.
I'm trying to turn my login form into an ajax one but am having difficulty achieving this using cakephp (I have created ajax login forms before just not through cake). I also can't find any tutorials online everywhere and so after a full weekend of struggling and to no avail I seek someone on heres help.
OK, onto the code.

Here is my controller code,

Code: Select all

public function login()
	{
		if(!empty($this->data)) {
			if($this->RequestHandler->isAjax()) {
				if($this->Auth->login()) {
					$response = array('success' => TRUE);
				} else {
					$response = array('success' => FALSE);
				}
			}
			$this->set('response', $response);
	   		$this->render('ajaxReturn', 'ajax');
		}
	}
My Javascript Code,

Code: Select all

function sendToPHP() {
	$.ajax({url:"/accounts/login",type:"POST", data:$('#loginForm').serialize(), dataType:"json", success:responseLogin, context:this});
}
function responseLogin(response)
{
	if(!response.success)
    	alert('hello');
    else
       alert('hello2');
}
I have over the course of the weeken changed the code dramaticaly and this is the end result which doesn't work - but i have no idea where i'm meant to be heading with this I might be completely wrong!
Can someone please help me?

Many thanks for your time.
Chris
Post Reply