Ajax with validate.js returning undefined result from php

95 Views Asked by At

Hello All, I have written a one demo code to check weather the given input user is exist in the list or not using ajax and validate.js, when I'm running the code all functions are executing fine but insted of getting response message in succes function it is jumping to the error function and giving undefined response as sent form php.

Here is my code:

$.validator.addMethod("checkUserExists", function(value, element){           
            alert("input checking");
            var inputElem = $('#hl_form :input[name="username"]'),
                data = { "username" : inputElem.val(),"check": "userCheck"},
                eReport = ''; //error report

            $.ajax(
            {
                type: "POST",
                url: "services.php",
                async: true,
                dataType: 'json',
                data: data, 
                success: function(result)
                {
                    alert(result);
                    console.log(result);
                    if (result.status !== 'true')
                    {
                      return '<p>This User is already registered.</p>';
                    }else{
                       return true;
                    }
                },
                error: function(xhr, textStatus, errorThrown)
                {
                    //alert('ajax loading error... ... '+url + query);
                    return false;
                }
            });

        }, 'User Alread exist in the DB'); 

My Validate.js Rules and and Message are

Validate Method Rule

username: {             
           required: true,          
           checkUserExists:true 
}

Validate.js Method Message

username: {             
           required: "Please enter your Username",           
           checkUserExists: "User... already exist"  
},

My Php Code (service.php)

<?php
    header('Content-Type: application/json');
    class form_services{
        public $sql;
        public $returnResult = array();
        function checkUser($requestedUser) {
            $registeredUser = array('xyz', 'abc', 'def', 'ghi', 'jkl');
            if( in_array($requestedUser, $registeredUser) ){
               $returnResult["status"] = 'false';
            }else{
                $returnResult["status"] = 'true';               
           }             
        return $returnResult;
        }


    } //Class Ends Here

    $checkRequest  = $_POST['check'];
    $frmServices = new form_services();
    $data = '';     
    switch ( $checkRequest) {       
    case 'userCheck':   $requestedUser  = $_REQUEST['username'];
                        $data = $frmServices->checkUser( $requestedUser);
                        echo json_encode($data);
                        break;      
          default:      echo json_encode($data);
                        break;
    }
    ?>

Please help me in resolving my issue, i'm getting undefined result in ajax call from php cod.

0

There are 0 best solutions below