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

Community
  • 1
  • 1
Daniel Harris
  • 1,805
  • 10
  • 45
  • 63

1 Answers1

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.

Daniel Harris
  • 1,805
  • 10
  • 45
  • 63