Complete php ajax user registration and user name verification instance

Source: Internet
Author: User

<! Doctype html public "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
<Title> insert title here </title>
<Script type = "text/webpage effects">
Function checkusername (input ){
Var name = input. value;
Var url = 'checkusername. php Tutorial? Username = '+ name;

Var xmlhttp = getxmlhttpobject ();
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readystate = 4 ){
$ ("Pmt_username"). innerhtml = xmlhttp. responsetext;
  }
 }
Xmlhttp. open ("get", url, true );
Xmlhttp. send (null );
}
Function getxmlhttpobject (){
Var xmlhttp = null;
Try {
// Firefox, opera 8.0 +, safari
Xmlhttp = new xmlhttprequest ();
} Catch (e ){
// Internet explorer
Try {
Xmlhttp = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Xmlhttp = new activexobject ("microsoft. xmlhttp ");
  }
 }
Return xmlhttp;
}
Function checkform (form ){
Try {
Assertnonempty (form. username );
Assertnonempty (form. pwd );
// Assertint (form. mobile );
} Catch (e ){
Return false;
 }
Return true;
}
Function assertnonempty (obj ){
If (obj. value = ''){
Var tds = gettdelems (obj );
Var text = tds [0]. innerhtml;
  
Tds [1]. innerhtml = text. slice (0,-1) + 'cannot be blank ';
Obj. focus ();
Obj. select ();
  
Throw new exception ();
 }
}
Function assertint (obj ){
Var v = obj. value;
If (/d {11}/. test (v )){
Alert ('OK ');
} Else {
Alert ('error ');
 }
If (number (v )! = V ){
Var tds = gettdelems (obj );
Var text = tds [0]. innerhtml;
  
Tds [1]. innerhtml = text. slice (0,-1) + 'should be a numerical value ';
  
Obj. focus ();
Obj. select ();
Throw new exception ();
 }
}
Window. onload = function (){
// Alert ($ ('Username'). type );
// $ ('ABC'). onkeydown = mobilekeydown;
}
Function gettdelems (obj ){
Var tr = obj. parentnode. parentnode;
Var tds = tr. getelementsbytagname ('TD ');
Return [tds [0], tds [2];
}
Function mobilekeydown (e ){
E = e | event;
Return e. keycode> 47 & e. keycode <59;
}

Function $ (id ){
Return document. getelementbyid (id );
}
Function checkcode (input ){
Var name = input. value;
Var url = 'checkcode. php? Code = '+ name;

Var xmlhttp = getxmlhttpobject ();
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readystate = 4 ){
$ ("Code"). innerhtml = xmlhttp. responsetext;
  }
 }
Xmlhttp. open ("get", url, true );
Xmlhttp. send (null );
}
</Script>
</Head>
<Body>
<Form method = "post" action = 'Register. Php' enctype = "multipart/form-data" onsubmit = 'return checkform (this) '>
<Table>
<Tr>
<Td> user name: </td>
<Td> <input type = 'text' name = 'username' onblur = 'checkusername (this) '> </td>
<Td id = 'pmt _ username'> </td>
</Tr>
<Tr>
<Td> Password: </td>
<Td> <input type = 'password' name = 'pwd'> </td>
<Td id = 'pmt _ pwd'> </td>
</Tr>
<Tr>
<Td> duplicate password: </td>
<Td> <input type = 'password' name = 'pwd2 '> </td>
<Td> </td>
</Tr>
<Tr>
<Td> Name: </td>
<Td> <input type = 'text' name = 'name'> </td>
<Td> </td>
</Tr>
<Tr>
<Td> Gender: </td>
<Td>
<Input id = 'sex _ m' checked type = 'Radio 'name = 'sex' value = 'male'> <label for = 'sex _ m'> male </label>
<Label> <input type = 'Radio 'name = 'sex' value = 'female'> Female </label>
</Td>
<Td> </td>
</Tr>
<Tr>
<Td> Age: </td>
<Td> <input type = 'text' name = 'age'> </td>
<Td id = 'pmt _ age'> </td>
</Tr>
<Tr>
<Td> Mobile Phone: </td>
<Td> <input type = 'text' name = 'mobile'> </td>
<Td> </td>
</Tr>
<Tr>
<Td> communication address: </td>
<Td> <input type = 'text' name = 'address'> </td>
<Td> </td>
</Tr>
<Tr>
<Td> Email Address: </td>
<Td> <input type = 'text' name = 'Email '> </td>
<Td> </td>
</Tr>
<Tr>
<Td> user Photo: </td>
<Td> <input type = 'file' name = 'photo'> </td>
<Td> </td>
</Tr>
<Tr>
<Td> date of birth: </td>
<Td> <input type = 'text' name = 'birthday'> </td>
<Td> </td>
</Tr>
<Tr>
<Td> verification code: </td>
<Td> <input type = 'text' name = 'verifycode' onblur = 'checkcode (this) '> </td>
<Td id = 'code'> </td>
</Tr>
<Tr>
<Td colspan = '3'> <input type = 'submit 'value = 'registration'> <input type = 'reset' value = 'reset'> </td>
</Tr>
</Table>
</Form>

</Body>
</Html>

Checkusername. Php file

<? Php
$ Username = $ _ get ['username'];
Mysql tutorial _ connect ('127. 0.0.1 ', 'root', '') or die ('could not connect:'. mysql_error ());
Mysql_select_db ('test ');
 
// When splicing an SQL statement, you must process the value entered by the user, replace the special character, and enclose it in quotation marks.
$ Username = htmlentities ($ username, ent_quotes );
$ SQL = "select count (*) from user where username = '{$ username }'";
 
$ Result = mysql_query ($ SQL );
If ($ row = mysql_fetch_array ($ result, mysql_num )){
$ Cnt = $ row [0];
If ($ cnt = 0 ){
Echo 'user name is not used ';
} Else {
Echo 'user name used, please change username ';
  }
 }
?>

Verifycode. php verification code program


<? Php
Session_start ();

$ Rnd = array_merge (range (0, 9), range ('A', 'z '));
Shuffle ($ rnd );

$ Code = implode ('', $ rnd );
$ Code = substr ($ code, 0, 4 );

$ _ Session ['verifycode'] = $ code;

Header ("content-type: image/png ");
$ Im = @ imagecreate (50, 25) or die ("cannot initialize new gd image stream ");
$ Background_color = imagecolorallocate ($ instant, 255,255,255 );
$ Text_color = imagecolorallocate ($ im, 233, 14, 91 );
Imagestring ($ im, 5, 5, 5, $ code, $ text_color );
Imagepng ($ im );
Imagedestroy ($ im );


?>
Checkcode. php checks whether the verification code entered by the user is consistent
<? Php
Session_start ();
 
$ Code = $ _ get ['code'];
 
Echo strtoupper ($ code) =$ _ session ['verifycode']? 'Verification code right': 'Verification code error ';
?>

Register. php registration handler

<? Php
Session_start ();

$ Filename = '';
If (isset ($ _ files ['photo']) {
$ Uploaddir = dirname (_ file _). directory_separator. 'Upload ';
$ Originfilename = $ _ files ['photo'] ['name'];
$ Extname = strtolower (substr ($ originfilename, strrpos ($ originfilename, '.') + 1 ));
 
$ Filename = time (). '.'. $ extname;
 
$ Validext = array ('jpg ', 'jpeg', 'GIF', 'PNG ');
If (! In_array ($ extname, $ validext )){
Errormsg ("incorrect file type ");
 }
 
If ($ _ files ['photo '] ['size']> 100*1024 ){
Errormsg ("The file is too large to exceed 100 kB ");
 }
$ Uploadfile = $ uploaddir. directory_separator. $ filename;
 
If (! Move_uploaded_file ($ _ files ['photo '] ['tmp _ name'], $ uploadfile )){
Errormsg ("photo Upload failed ");
 }
}

$ Code = $ _ post ['verifycode'];
If ($ code! = $ _ Session ['verifycode']) {
Errormsg ("incorrect verification code ");
}

Mysql_connect ('2017. 0.0.1 ', 'root ','');
Mysql_select_db ('test ');
Mysql_query ('set names utf8 ');

$ Username = addslashes ($ _ post ['username']);
$ Pwd = md5 ($ _ post ['pwd']);
$ Name = addslashes ($ _ post ['name']);
$ Sex = addslashes ($ _ post ['Sex ']);
$ Age = intval ($ _ post ['age']);
$ Mobile = addslashes ($ _ post ['mobile']);
$ Address = addslashes ($ _ post ['address']);
$ Email = addslashes ($ _ post ['email ']);
$ Photo = $ filename;
$ Birthday = addslashes ($ _ post ['birthday']);

$ SQL = "insert into user (username, pwd, name, sex, age, mobile, address, email, photo, birthday)
Values ('$ username',' $ pwd', '$ name',' $ sex ', $ age,' $ mobile ',' $ address ',' $ email ', '$ photo',' $ birthday ')";

If (! Mysql_query ($ SQL )){
Errormsg ('database Tutorial writing failed! ');
}

$ SQL = 'select * from user ';
$ Res = mysql_query ($ SQL );
Echo'
<Table>
<Tr>
<Td> user name </td>
<Td> Name </td>
<Td> Gender </td>
<Td> age </td>
<Td> mobile phone </td>
<Td> communication address </td>
<Td> Email address </td>
<Td> date of birth </td>
<! -- <Td> photo </td> -->
</Tr>
';
While ($ row = mysql_fetch_assoc ($ res )){
$ Photo = $ row ['photo']? " ":'';
Echo"
<Tr>
<Td >{$ row ['username']} </td>
<Td >{$ row ['name']} </td>
<Td >{$ row ['sex']} </td>
<Td >{$ row ['age']} </td>
<Td >{$ row ['mobile']} </td>
<Td >{$ row ['address']} </td>
<Td >{$ row ['email ']} </td>
<Td >{$ row ['birthday']} </td>
<! -- <Td> $ photo </td> -->
</Tr>
";
 
}

Echo '</table> ';


Function errormsg ($ str ){
Die ('<script type = "text/javascript"> alert ("'. $ str. '"); </script> ');
}


?>

Simplest database structure

Drop database if exists test;
Create database test character set utf8 collate utf8_general_ci;

Use test;

Create table user (
Username char (10) primary key
, Pwd char (32) not null
, Name char (10) not null
, Sex char (1) not null
, Mobile char (11)
, Age smallint
, Address varchar (50)
, Email varchar (30)
, Photo varchar (20)
, Birthday date
);

Http://down.111cn.net/down/code/php/qitayuanma/2010/1220/22331.html

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.