0

I'm using jQuery Validate plugin for field validations on my registration page.

I currently have this code in my validation JS for checking whether the entered username already exists or not:

"remote": {
    url: "../assets/php/checkUsername.php",
    type: "post",
    data: {
        username: function() {
            return $('#register-form :input[name="username"]').val();
        }
    }
}

checkUsername.php basically returns true or false based on existence of entered username. This works all fine but I want to optimize it.

Now I have core.php file that already has function called account_exists($username) on it. This function basically returns true or false based on existence of given username as well.

My question is, how can I get rid of checkUsername.php altogether and instead use account_exists function that is in my core.php file?

core.php obviously contains a lot more things so I'm not sure how I can tell my validation script to call the specific function from the core.php.

8
  • FYI you don't need the anonymous function: data: { username: $('#register-form :input[name="username"]').val() } Commented Jul 5, 2016 at 15:37
  • could you check with a get at start of the file? if(isset($_GET['checkUsername'])){ account_exists(); } Commented Jul 5, 2016 at 15:38
  • 2
    You send data to the PHP script which tells the PHP script which function you want to use and return information from. You might put your functions in a case statement to facilitate this. switch($_POST['function']) { case 'username'.... etc. Commented Jul 5, 2016 at 15:39
  • JS cannot execute PHP code. you can only do ajax requests to the server, which means you HAVE to point your JS code at your .php file. Commented Jul 5, 2016 at 15:40
  • @JayBlanchard Is there any way of going about it without altering my core.php? I just want to edit my validation JS. Commented Jul 5, 2016 at 15:41

1 Answer 1

0

You can pass another parameter so core.php knows what you want to do

"remote": {
            url: "../assets/php/core.php",
            type: "post",               
            data: {
                action: 'check_account',
                username: function() {
                    return $('#register-form :input[name="username"]').val();
                }
            }
        }

Of course you will also have to modify core.php so it executes the required code if action=='check_account':

if ($_POST['action']=='check_account')
{
...do something...
}
Sign up to request clarification or add additional context in comments.

3 Comments

The only problem you have here is you didn't change the data statement properly, so the answer is fundamentally wrong.
@JayBlanchard, it would work. See the second example here: jqueryvalidation.org/remote-method
To the OP of the question and the answer: You do not need to send the value of the field as part of the data parameter since the value of the field being validated is already being sent by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.