PHP+Ajax Detects if an instance tutorial already exists when a user name or message is registered
- 2021-07-16 02:06:42
- OfStack
PHP+Ajax Detecting whether a user name or email already exists at the time of registration is a common and important function in forums or member systems. This paper briefly describes the implementation method of this function in the form of examples. The specific steps are as follows:
1. PHP detection page
The code for the check. php page is as follows:
<script type="text/javascript" src="jiance.js"></script>
<form name="myform" action="" method="get">
User name: <input name="user" value="" type="text" onblur="funtest100()" />
<div id="test100"></div>
</form>
2. Ajax validation page
The code for the check. js page 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;// Get the contents of the text box
S_xmlhttprequest();
xmlHttp.open("GET","jcfor.php?id="+f,true);// Discovery request
xmlHttp.onreadystatechange = byphp;// Ready to execute
xmlHttp.send(null);// Send
}
function byphp(){
// Judgment state
if(xmlHttp.readyState==1){//Ajax Status
document.getElementByIdx_x_x('test100').innerHTML = " Loading ";
}
if(xmlHttp.readyState==4){//Ajax Status
if(xmlHttp.status==200){// Server-side state
var bytest100 = xmlHttp.responseText;
//alert(bytest100);
document.getElementByIdx_x_x('test100').innerHTML = bytest100;
}
}
}
3. PHP validation page
The code for the chkfor. php page 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 " The user name can be used ";
}
}
?>
I hope the example described in this paper is helpful to the development of PHP program.