0

I have some code that validates a form using Prototype but I want to add a jQuery ajax function to check if the username already exists in the database. However, the value being returned by jQuery is always an object and not the boolean of whether the username exists or not. Here is my code so far:

validation.js

//Using Prototype
Validation.addAllThese([

    ['validate-username', 'Username already exists. Please choose another one.', 
      function (v) {     //v is the value of the username field in the form.

        //Using jQuery
        var match = jQuery.ajax({
                        url: "/php/ajax/check_username_exists.php",
                        data: {username: v},
                        async: false
                    });
        return (match.responseText == v);

]);

check_username_exists.php

<?php
include '../library.php';
include '../config.php';

//Echo string username if matches
echo select_row("USERNAME", "members", "USERNAME='".$_GET['username']."'");
?>

I have checked other threads on StackOverflow including this one but none seem to fix the problem.

Thanks

7
  • Yes, jQuery.ajax returns a jqXHR object. That's what it is supposed to do.
    – Kevin B
    Commented May 23, 2013 at 21:16
  • So how do I return the value of data in the validation function? Commented May 23, 2013 at 21:17
  • @Daniel you can't return from an ajax callback. Any work that relies on the result of the ajax call must be done in the callback Commented May 23, 2013 at 21:18
  • There is no "done" option, use "success" instead. Commented May 23, 2013 at 21:20
  • Can you show us your server side code. I think maybe you are getting JSON data from server. Commented May 23, 2013 at 21:30

1 Answer 1

1

OK, Here is the solution code:

var match = jQuery.ajax({
    url: "/php/ajax/check_username_exists.php",
    data: {username: v},
    async: false
});
return (match.responseText != v);

Return was supposed to be false when invalidated. So I had it returned true if the username existed which was supposed to be false. A simple change from == to != fixed the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.