SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting undefined after the ajax call

    Hi

    I am trying to build Ajax & Codeigniter login form. My first objective is if the user enter wrong username or password, to inform him with jquery, without relaoding the page. But after the ajax call, I am getting undefined in the console instead of my error message. Here is the code from the JavaScript (jQuery)

    Code:
    formLogin.on('submit', function(e,errorMessage){
                   
               $.ajax({
                type: 'POST',
                url: 'loginCheck',
                data: $(formLogin).serialize(),
                dataType: 'json',
                success: function(){ 
                    console.log('my message ' + errorMessage);
                    
                    }
                });
         
                  e.preventDefault();
           });
    And here is the code from the Codeigniter controller.

    PHP Code:


    if .....
    // the code that checks for the username and password against the database and user is found....

    else {   
    // user is not found, so populate the error message....

                        
    $errorMessage "Wrong Username or Password";               
                         echo 
    json_encode($errorMessage);
                         
                   
    // $this->cms($errorMessage);
                   
                

    In the console log, I am getting the following message:

    my message undefined.

    Any help will be deeply appreciated.

    Regards, zoreli

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The problem is where you placed your errorMessage argument:

    Code:
    formLogin.on('submit', function(e,errorMessage){
    Whenever you use the $.ajax method all return arguments need to be part of the success(), error() or complete() methods as they are what return the strings from your PHP source, see the below:

    Code JavaScript:
    formLogin.on('submit', function(e) {
        e.preventDefault();
     
        $.ajax({
            type : 'POST',
            url : 'loginCheck',
            data : $(formLogin).serialize(),
            dataType : 'json',
            success : function(errorMessage) { 
                console.log('my message ' + errorMessage);
            }
        });
    });
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Chris

    Thanks for your help, that did the trick.

    Regards, zoreli


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •