Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6981597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:10:40+00:00 2026-05-27T18:10:40+00:00

I have the following method which allows a user to login into my application:

  • 0

I have the following method which allows a user to login into my application:

public function login()
{   
    if($this->Auth->user())
    {
        $this->Session->setFlash(__('Already logged in...'), 'default', array(), 'auth');

        $this->redirect(array('controller'=>'pages','action'=>'display','home'));
    }

    if ($this->request->is('ajax'))
    {
        $this->layout = 'ajax';

        if ($this->Auth->login())
        {

        }
        else
        {

        }
    }
    else if ($this->request->is('post'))
    {
        if ($this->Auth->login())
        {
            return $this->redirect($this->Auth->redirect());
        }
        else
        {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

It allows a user to login either using a post request or via an ajax request. However with regards to the ajax requests how do I pass the results back using JSON? So for example if the user enters the wrong details pass back an error message?

I have the jQuery AJAX already setup so I just need to do some extra logic in the success method to deal with the return which will either show the error message from the server or do a redirect again based on the return from the server.

e.g.

    $('form').live('submit', function (event) {

    // Declare the form
    var form = $(this);

    // Stop the form from doing a native postback
    event.preventDefault();

    // Get the data from the form
    var data = form.serialize();

    $.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: data,
        success: function (responseHtml) {

            // If correct login details
            if(success){
                 window.location.href('INSERT LOCATION TO REDIRECT TO FROM SERVER');
            } else {        
                 alert('INSERT MESSAGE FROM THE SERVER');
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {

                alert('Error!');

        }

    });

});

Can anyone help? I’m using CakePHP 2.0 and all of the tutorials I have seen on the net and on here have either been outdated or much too long-winded for what I’m trying to achieve.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:10:41+00:00Added an answer on May 27, 2026 at 6:10 pm

    Edit: you need to use the following php code

        if ($this->Auth->login())  
        {  
             $arr = array("login" => "true" , "redirect" => "/redirect_url");
             echo json_encode($arr);
        }  
        else  
        {  
             $arr = array("login" => "false" , "error" => "Invalid Login Credentials");
             echo json_encode($arr);
        } 
    

    on the javascript side you need to modify your javascript to handle json values using jQuery.getJSON. example of jQuery.getJSON is availabe here http://api.jquery.com/jQuery.getJSON/

    use this jQuery code

    <script  type="text/javascript">
    
    
      $.getJSON("login.php", function(data) {
                      if(data.login)
                      {
                          //redirect user with data.redirect
                      }
                      else
                      {
                          //display error   with data.error
                      }
       })
    
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method which is not capturing anything from the user.If I
I have a CATiledLayer into which I render content in the following method -
I have developed a Java EE web application. This application allows a user to
I have developed an application which allows the user to switch between themes. I'm
I have the following method which serialises an object to a HTML tag. I
I have a view with which I have a button calling the following method.
My problem is the following. I have a method which simply takes an XML
I have the following code which works just fine when the method is POST,
Which of the following has the best performance? I have seen method two implemented
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.