views:

56

answers:

1

I have written a PHP script that checks whether a domain is available for registration or not.

To automate the process, I have also written a js script that automatically sends AJAX calls to the PHP script and tells the user whether the domain is available or not, without submitting the form:

$(document).ready(function() {

    function Domain() {    
        this.name = '';
        this.dotComRegistered = 1;
        this.dotNetRegistered = 1;
        this.dotOrgRegistered = 1;
    }

    Domain.prototype.check = function(input) {
        this.name = input;

        if (this.name.length >= 3 && this.name.length <= 63) {        
            $.get('check.php', { d : this.name }, function(json) {

                alert(json);

                this.dotComRegistered = $.evalJSON(json).com;
                this.dotNetRegistered = $.evalJSON(json).net;
                this.dotOrgRegistered = $.evalJSON(json).org;

            });
        }
    }

    var domain = new Domain();

    var input = ''

    $('#domain').keyup(function() {

        input = $('#domain').val();
        domain.check(input);

    });

    $('form').submit(function() {

        input = $('#domain').val();
        domain.check(input);
        return false;

    });

});

As you can see I created an object called Domain which represents a domain name. The object has just one method (besides the constructor) that sends AJAX request to the PHP script (which returns json).

The problem is that the Domain.prototype.check() method doesn't work (I don't get an alert window) and I don't know where's the problem. When I place the AJAX call outside of the method it works, so that isn't the problem.

I'm an OOP beginner so maybe I used some wrong syntax to write the Domain object (I'm reading a book from John Resig about OOP in JavaScript right now).

The #domain is the input field for domain names.

+2  A: 

have you stepped through with Firebug? Set a breakpoint at the point the request is made and step through from that point. Where does the dn come from in your code?

Also, is there a specific reason that your Domain function is in $(document).ready()? It doesn't really need to be there (you might consider namespacing your classes too).

Russ Cam
Ah. I was rewriting "dn" to "name" in the script and I forgot to rewrite it there. Now it works :)
Richard Knop
@Richard - glad to have helped. P.S. which Resig book are you reading?
Russ Cam
This one: http://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273
Richard Knop
That's a great book. I'd recommend signing up for the MEAP for his next book after you've finished that one- Secrets of the JavaScript Ninja - http://manning.com/resig/ Started reading it yesterday, very enlightening :)
Russ Cam
About namespacing, I am not that far yet :) so I don't know how to do it.
Richard Knop
Namespacing is essentially a pattern for avoiding pollution of the global namespace by providing functions as properties of an object literal or invoking a function that returns an object literal and assigning that to a global variable. Take a look at - http://www.dustindiaz.com/namespace-your-javascript/ , http://www.brainonfire.net/blog/javascript-object-literal-namespace/ and http://www.davidpirek.com/blog.aspx?n=Object-Literal-JavaScript-Constructer-or-Douglas-Crockfords-Module-Pattern
Russ Cam