search
HomeBackend DevelopmentPHP Tutorial七、注册页面实现

PHP经典项目案例-(一)博客管理系统4

本篇使用Ajax实现页面无刷新验证用户名是否存在。

七、注册页面实现

1、注册页面设计

register.php部分代码:

<tr>    <!-- 注册表 -->       <td colspan="3" valign="baseline" style="BACKGROUND-IMAGE: url( images/bg.jpg); VERTICAL-ALIGN: middle; HEIGHT: 450px; TEXT-ALIGN: center">
<br>        <form name="myform" action="register_deal.php" method="post">           <table width="85%" border="1" align="center" cellpadding="3" cellspacing="2" bordercolor="#FFFFFF" bgcolor="#FFFFFF" class="i_table">             <tr align="left" bgcolor="#EFF7DE">               <td height="22" colspan="2" bgcolor="#EFF7DE" class="right_head"><span class="tableBorder_LTR">必填内容</span></td>             </tr>             <tr bgcolor="#FFFFFF">               <td width="22%" align="right" valign="middle" class="'f_one'"> 用户名</td>               <td width="78%" align="left" class="'f_one'">
<input name="'txt_regname'" type="text" id="txt_regname" value="''" size="20" maxlength="14" onblur="javascript:chkUserName();">               <label id="l1">*</label><label id="ll1" class="label2"></label>              </td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" valign="middle"> 真实姓名</td>               <td align="left"> <input name="'txt_regrealname'" type="'text'" id="txt_regrealname" size="'20'" maxlength="'75'" onblur="javascript:chkRealName()">                              <label id="l4">*</label><label id="ll4" class="label2"></label>
</td>             </tr>             <tr>             </tr>
<tr bgcolor="#FFFFFF">               <td align="right" valign="middle"> 密码</td>               <td align="left"> <input name="'txt_regpwd'" type="'password'" id="txt_regpwd" size="20" maxlength="75" onblur="javascript:chkPwd();">               英文字母或数字等不少于3位<label id="l2">*</label><label id="ll2" class="label2"></label>
</td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" valign="middle"> 确认密码</td>               <td align="left"> <input name="'txt_regpwd2'" type="'password'" id="txt_regpwd2" size="20" maxlength="75" onblur="javascript:chkRePwd()">               <label id="l3">*</label><label id="ll3" class="label2"></label>
</td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right"> 出生日期</td>               <td align="left"> <span class="word_grey">                 <input name="txt_birthday" type="text" id="birth" onblur="javascript:chkBirth()">               (日期格式为:yyyy-mm-dd)<label id="l6">*</label><label id="ll6" class="label2"></label></span>
</td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" valign="middle"> Email</td>               <td align="left"> <input name="txt_regemail" type="text" id="txt_regemail" value="''" size="35" maxlength="75" onblur="javascript:chrEmail()">               <font color="'#000000'">公开邮箱 <label id="l5">*</label><label id="ll5" class="label2"></label></font>
</td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right">所在城市</td>               <td align="left">                <select name="txt_province" id="txt_province" onchange="javascript:initcity();">                   <script>creatprovince();</script>                 </select>                 <select name="txt_city" id="txt_city"> </select>               <font color="red">*</font> </td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" valign="middle" class="'f_one'"> 选择头像:</td>               <td align="left" class="'f_one'"><table width="106" cellpadding="0" cellspacing="0">                   <tr>                     <td width="10" height="47">					</td>                     <td width="80"><img  src="images/head/0.gif" name="img" id="image"    style="max-width:90%"  style="max-width:90%" alt="七、注册页面实现" ></td>                     <td width="53" rowspan="2" align="center"><font color="red">*</font></td>                   </tr>                   <tr>                     <td> </td>                     <td> <select name="txt_ico" size="1" id="txt_ico" onchange="javascript:showlogo()">                         <option value="0.gif" selected>头像1                        </option>
<option value="1.gif">头像2                        </option>
<option value="2.gif">头像3                        </option>
<option value="3.gif">头像4</option>                         <option value="4.gif">头像5</option>                         <option value="5.gif">头像6</option>                       </select> </td>                   </tr>               </table></td>             </tr>             <tr align="left" bgcolor="#EFF7DE">               <td height="22" colspan="2" class="right_head"><span class="tableBorder_LTR">选填内容</span></td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" class="'f_one'"> 性别</td>               <td align="left" class="'f_one'"> <select name="txt_regsex" id="txt_regsex">                   <option value="1">男</option>                   <option value="2">女</option>                   <option value="0" selected>保密</option>               </select>
</td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" class="'f_one'">QQ</td>               <td align="left" class="'f_one'"><input name="'txt_regqq'" type="text" id="txt_regqq" value="''" size="20" maxlength="14"></td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" class="'f_one'"> 个人主页</td>               <td align="left" class="'f_one'"> <input name="'txt_reghomepage'" type="text" id="txt_reghomepage" value="''" size="40" maxlength="75">
</td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" valign="middle" class="'f_one'">个性化签名</td>               <td align="left" class="'f_one'"><textarea name="'txt_regsign'" cols="50" rows="'4'" id="txt_regsign"></textarea></td>             </tr>             <tr bgcolor="#FFFFFF">               <td align="right" class="'f_one'"> 自我简介</td>               <td align="left" class="'f_one'"><textarea name="txt_regintroduce" cols="50" rows="4" id="txt_regintroduce"></textarea></td>             </tr>           </table>           <br>             <input type="'submit'" name="'regsubmit'" value="'提" onclick="return check()">              <input name="reset" type="reset" class="btn_grey" value="重 填">      </form>      </td>     </tr> 

2、输入框失去焦点后调用的javascript函数实现:fun.js:

function chkUserName(){	var c = document.getElementById('txt_regname');	var d = c.value;	var id;	if(d==""){				document.getElementById('l1').innerText="请输入用户名";		document.getElementById('ll1').innerText="";	}else{		var xmlObj;		xmlObj = new XMLHttpRequest();			xmlObj.open('POST','chk.php?d='+d,true);		xmlObj.onreadystatechange = callBackFun;		xmlObj.send(null);		function callBackFun() {			if(xmlObj.readyState == 4&&xmlObj.status ==200){				if(xmlObj.responseText=='y'){					document.getElementById('l1').innerText="×";					document.getElementById('ll1').innerText="";				}else{					document.getElementById('l1').innerText="";					document.getElementById('ll1').innerText="√";				}			}		}	}}function chkRealName(){	var c = document.getElementById('txt_regrealname');	var d = c.value;	if(d==""){				document.getElementById('l4').innerText="请输入真实姓名";		document.getElementById('ll4').innerText="";	}else{		document.getElementById('l4').innerText="";		document.getElementById('ll4').innerText="√";	}}function chkPwd(){	var p = document.getElementById('txt_regpwd').value;	var c = document.getElementById('ll1').innerText;	if(c=="√"){		if(p==""){			document.getElementById('l2').innerText="请输入密码";		}		else if(p.length<br><h2 id="Ajax验证用户名使用的处理文件chk-php">3、Ajax验证用户名使用的处理文件chk.php:</h2><p>'</p><pre name="code" class="php"><?php require_once &#39;Conn/SqlHelper.class.php&#39;;    $chk = $_REQUEST[&#39;d&#39;];    $sqlHelper = new SqlHelper();    $sql = "select * from tb_user where regname=&#39;$chk&#39;;";    $res = $sqlHelper->execute_dql($sql);    $s = $res->fetch_assoc();    if(count($s)!=0){        echo 'y';    }else{        echo 'n';    }

4、注册后将用户添加到数据库registerdeal.php

<?phpsession_start ();include "Conn/SqlHelper.class.php";$sqlHelper = new SqlHelper();$UserName=$_POST[&#39;txt_regname&#39;];$sql="select * from tb_user where regname = &#39;$UserName&#39;";$res = $sqlHelper->execute_dql($sql);$result=$res->fetch_assoc();if (count($result)!=0){	echo ("<script>alert(&#39;用户名已被注册!&#39;);history.go(-1);</script>");	exit();}$_SESSION['username']=$_POST['txt_regname'];$regname=$_POST['txt_regname'];$regrealname=$_POST['txt_regrealname'];$regpwd=$_POST['txt_regpwd'];$regbirthday=$_POST['txt_birthday'];$regemail=$_POST['txt_regemail'];$regcity=$_POST['txt_province'].$_POST['txt_city'];$regico=$_POST['txt_ico'];$regsex=$_POST['txt_regsex'];$regqq=$_POST['txt_regqq'];$reghomepage=$_POST['txt_reghomepage'];$regsign=$_POST['txt_regsign'];$regintroduce=$_POST['txt_regintroduce'];$ip=getenv(REMOTE_ADDR);$sql = "Insert Into tb_user (regname,regrealname,regpwd,regbirthday,regemail,regcity,regico,regsex,regqq,reghomepage,regsign,regintroduce,ip,fig)"." Values ('$regname','$regrealname','$regpwd','$regbirthday','$regemail','$regcity','$regico','$regsex','$regqq','$reghomepage','$regsign','$regintroduce','$ip',0)";$INS=$sqlHelper->execute_dml($sql);echo "<script> alert(&#39;用户注册成功!&#39;);</script>";echo "<script> window.location=&#39;index.php&#39;;</script>";?>


至此,用户注册已经实现。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software