Php+ajax detection of user name or mail registration is already existing in the forum or membership system is a common important function. This paper describes the implementation method of this function in the form of an instance. The specific steps are as follows:
First, the PHP test page
The check.php page code is as follows:
<script type= "Text/javascript" src= "jiance.js" ></script> <form name= "MyForm" action= "method="
Get >
username: <input name= "user" value= "type=" text "onblur=" funtest100 () "/> <div id="
test100 "> </div>
</form>
Second, the AJAX verification page
The Check.js page code is as follows:
var xmlHttp;
function S_xmlhttprequest () {
if (window). ActiveXObject) {
xmlHttp = new ActiveXObject (' microsoft.xmlhttp ');
} else if (window. XMLHttpRequest) {
xmlHttp = new XMLHttpRequest ();
}
}
function funtest100 () {
var f = document.getelementsbytagname_r (' form ') [0].user.value;//gets text box content
S_ XMLHttpRequest ();
Xmlhttp.open ("Get", "jcfor.php?id=" +f,true);/Open Request
Xmlhttp.onreadystatechange = byphp;//ready to execute
Xmlhttp.send (null);//Send
}
function byphp () {
//Judge status
if (xmlhttp.readystate==1) {//ajax State
document.getelementbyidx_x_x (' test100 '). InnerHTML = "Loading";
}
if (xmlhttp.readystate==4) {//ajax state
if (xmlhttp.status==200) {//server-side State
var bytest100 = Xmlhttp.responsetext;
alert (bytest100);
document.getelementbyidx_x_x (' test100 '). InnerHTML = bytest100;}}
Third, PHP verification page
The chkfor.php page code is as follows:
<?php
if ($_get[id]) {sleep
(1);
$conn =mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Test ', $conn);
$sql = "SELECT * from ' user ' WHERE ' name ' = ' $_get[id] '";
$q =mysql_query ($sql);
if (Is_array (Mysql_fetch_row ($q)) {
echo "user name already exists";
} else{
echo "User name can be used"
;
}
? >
I hope the example described in this article will help you with PHP program development.