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.