Ajax Entry Detection User name instance and implementation method (1/2)

Source: Internet
Author: User
Tags php tutorial

Ajax Entry Detection User name instance and implementation method

<form action= "#" method= "POST" name= "Iform" >
<p><label for=nickname> Username: </label><input id=nickname name=nickname placeholder= "Enter user name here" ><span Id=tips Tutorials ></span></p>
</form>


We also need a back-end page to determine if the input nickname exists (in this case, for example, PHP tutorial):

...

if (isset ($_get[' entryname ')) {
$entryname =$_get[' EntryName '];
}else{
$entryname = ' data null ';
}
$sql =sprintf ("select * from I_test_ajax where nickname= '%s '", $entryname);
$res = $iajax->query ($sql);
Sleep just to show the effect of readstate==1
Sleep (1);
if (($res->num_rows) >0) {
echo "Sorry!" This nickname already exists:(";
}else{
Echo, Congratulations! This nickname can be registered:) ";
}


...
The rest is to be handled by Ajax. XMLHttpRequest, an object that has to be mentioned, the most core of Ajax is also the bottom of the object. Sadly, it is a standard for the consortium, but Microsoft IE has been very self-Microsoft IE. What to do? It is of course a way to harmonize them. Microsoft IE supports the ActiveXObject (' Microsoft.XMLHTTP ') object, which is simple:

//Compatible XMLHttpRequest objects
Ixhr:function () {
if (window.activexobject) {
Xhr=new ActiveXObject (' microsoft.xmlhttp ');
}else if (window.xmlhttprequest) {
Xhr=new XMLHttpRequest ();
}else{
return null;
}
}

The compatible XMLHttpRequest object is implemented, then write a simple onblur event that, when the value is entered, the form loses focus and starts to judge and quickly returns a message to the foreground. The code is as follows:

Execution when the focus is triggered

document.forms[' iform '].nickname.onblur = function () {
The value entered
var val=document.forms[' Iform '].nickname.value;
The judgment of the user name
if (!/^[a-za-z0-9_]{3,16}$/.test (val)) {
Alert (' Please enter a nickname that consists of a 3~16 in English, numerals and underscores! ');
return false;
}
Initialize the XHR.
IBASE.IXHR ();
The original need for a new open judgment page uses get to use asynchronous
Xhr.open (' Get ', '/demo/ajax/iajax20110306_query.php?entryname= ' +val,true);
Associated with the ReadyState property, it triggers when the readystate changes
Xhr.onreadystatechange=returnfun;
Send data after asynchronous processing completes (for example, some that need to be executed after the focus event)
Xhr.send (NULL);
}


Home 1 2 last page
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.