skip to Main Content

I am gonna to test plugin Validation especially method “remote”. I made test example that consists from HTML file with following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script>
        $(function(){
            $('#myForm').validate({
                rules:
                {
                    name:
                    {
                        required:true,
                        remote:'nameCheck1.php'
                    },
                    email:
                    {
                        required:true,
                        email:true
                    }
                }
            }); 
        });
    </script>
</head>
<body>
    <form action="http://modx.megabulk.ru" method="post" id="myForm">
        <input type="text" name="name" placeholder="имя">
        <br><br>
        <input type="text" name="email" placeholder="e-mail">
        <br><br>
        <input type="submit">
    </form> 
</body>
</html>

And PHP file with following code:

<?php
    $users = array ("Poul", "Anna", "Malika", "Jack", "Elena");
    $newUser = $_POST['name'];


    if (in_array($newUser, $users)) 
    {
        return false;
    }
    else
    {
        return true;
    }
?>

And then I began to test my form. In browser console I see the following error:

TypeError: undefined is not an object (evaluating 'b.apply').  Exception occurred when checking element , check the 'remote' method.

I thought that my plugin is old and tried to use last version but I got the same error.

Can anyone tell me what I do wrong? How can I fix this bug?

Thanks in advance!

2

Answers


  1. Chosen as BEST ANSWER

    The following errors were made in my code

    • By default, data is passed through an array GET and I took them through POST
    • User gaetanoM was right I should use not return but echo in handler
    • I used slim version of jQuery that which does not contain ajax methods

    By fixing all these bugs I got right result.


  2. You are missing a script file, including this script should solve the issue.

    <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search