0

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.

SaaD Ahmed
  • 41
  • 6
  • What is the exact error message you are receiving? – aynber Jun 28 '19 at 17:55
  • Look at the Network tab in your browser's Developer Console, and check the Headers and Response tabs of the request to see what is being sent and received. – aynber Jun 28 '19 at 18:04
  • My false part is exicuting, That is **User... already existt** and it is due to ajax is not getting result in success function. – SaaD Ahmed Jun 28 '19 at 18:13
  • @aynber in **headers tab** I'm getting **Status Code: 200 OK** and In **Response Tab** I'm getting **{"status":"true"}** – SaaD Ahmed Jun 28 '19 at 18:23

0 Answers0