


PHP+javascript implements user registration module (source code attached)
php javascript implements user registration module (source code attached)
1. Function introduction
Use language: html + javascript ajax php
Backend database: MySQL
(* There is no style involved here )
Successful registration process:
① Registration page (register.html), this page provides a form to collect user information.
② After submission, go to the register.php page and use php to add the registration information to the database.
2. Implementation code
(1) Create the user information table in the MySql database
Requirements:Create user information table:
Code:
CREATE TABLE xxx_user( uid INT PRIMARY KEY AUTO_INCREMENT, uname VARCHAR(32), upwd VARCHAR(32), email VARCHAR(64), phone VARCHAR(16), gender INT #性别 0-女 1-男 );
(2) HTML page layout code
Requirements: Create a register.html (non-ajax) and provide the following controls (form)——
● Login name-text box
● Login Password - password box
● Confirm password - password box
● User email - email
● Contact information - text box
● User gender - Drop-down box
● Registration button
Code
<form action="./data/users/register.php" method="post"> <!-- 1.注册信息不用异步加载,直接提交表单;失去焦点时验证用户名密码是否正确,再用ajax异步加载数据; 2.form表单作用:收集用户信息并提交给服务器; 3.属性action作用:定义表单被提交时发生的动作,通常定义的是服务器上处理程序的地址(url),提交到注册的php文件,默认提交给本页; 4.属性method作用:指定表单数据的提交方式。 get(默认值)——明文提交,待提交的数据会显示在地址栏上; post——隐式提交,提交的数据不会显示在地址栏上。 --> <!--控件提交信息,嵌套在form标记里面--> <!--登录名称-文本框--> <p> 登录名称:<input type="text" id="uname" name="uname" onblur="check_name()"> <!--提示用户名是否一致的位置--> <span id="uname-show"></span> <!--提交数据时提交name属性的值,点击submit时,name属性通过表单form提交数据,同步提交数据--> </p> <!--登录密码-密码框--> <p> 登录密码:<input type="password" id="upwd" name="upwd"> <!--name值与id值可以重复,name值用于提交给服务器,id值在前端用--> </p> <!--确认密码-密码框--> <p> 确认密码:<input type="password" id="cpwd" name="cpwd" onblur="check_pwd()"> <!--onblur为失去焦点属性,调用判断密码是否一致的函数--> <!--用于提示两次密码是否一致的位置--> <span id="pwd-show"></span> </p> <!--用户邮箱-电子邮件--> <p> 电子邮箱:<input type="email" name="email"> <!--type="email" 可做简单的格式验证--> </p> <!--联系方式-文本框--> <p> 手机号码:<input type="text" name="phone"> </p> <!--用户性别-下拉框--> <p> 用户性别: <select name="gender"> <option value="1">男</option> <option value="0">女</option> </select> <!--下拉列表和选项,往数据库中插入的是value的值--> </p> <!--注册按钮--> <p> <input type="submit" value="注册"> <!--submit按钮的表单提交数据,是同步访问数据的方式--> </p> </form>
(3) Create register.php
Requirements: ① In init.php, encapsulate the reused connection database
The code is as follows:
<?php //data/init.php //创建到服务器的连接,连接数据库 $conn=mysqli_connect("127.0.0.1","root","","knewone",3306); $sql="SET NAMES UTF8"; mysqli_query($conn,$sql);
Requirements: ② Receive the data submitted by register.html, insert it into the database, and then give a prompt
The code is as follows:
<?php //data/users/register.php #1.获取请求提交的数据 $uname=$_REQUEST["uname"]; //uname值就是前端页面中name属性的值 $upwd=$_REQUEST["upwd"]; //确认密码不用获取,获取一个密码就行 $email=$_REQUEST["email"]; $phone=$_REQUEST["phone"]; $gender=$_REQUEST["gender"]; #2.连接到数据库 require("../init.php"); #3.拼sql语句并执行 $sql="insert into xxx_user(uname,upwd,email,phone,gender)values('$uname','$upwd','$email','$phone','$gender')"; //字段值 外面用双引号,里面用单引号 $result=mysqli_query($conn,$sql); //执行sql语句 #4.根据执行结果给出响应 if($result==true){ //函数返回值 echo "注册成功"; }else{ echo "注册失败"; };
(4) JavaScript code
Requirements:① Encapsulate functions that can be reused
The code is as follows:
<script> //1.封装函数,获取id值 function $(id){ return document.getElementById(id); } //2.创建xhr对象 function createXhr(){ var xhr=null; if(window.XMLHttpRequest){ xhr=new XMLHttpRequest(); //标准创建 }else{ //IE8以下的创建方式 xhr=new ActiveXObject("Microsoft.XMLHttp"); } return xhr; } </script>
Requirements: ② Implement the function of verifying the repeatability of user names and the consistency of two passwords on the front-end page
The code is as follows:
<script src="./js/common.js"></script> <script> //1.完成用户名称的重复性验证(异步,检查数据库中是否已存在当前用户名) //异步请求数据,因为还要输入下面的数据,不能跳转到php页面去验证 function check_name(){ //1.创建XHR对象 创建异步对象 var xhr=createXhr(); //调用common.js中封装好的函数 //2.创建请求 var uname=$("uname").value; //获取输入框里的值,把用户名传到后端,再查询 var url="./data/users/check-name.php?uname="+uname; xhr.open("get",url,true); //查询用户名称,用get方法就行,去数据库查询,看用户名是否已经存在 //查询用get就行,向服务器提交数据时再用post //3.设置回调函数,监听状态 //参数true,异步 xhr.onreadystatechange=function(){ if(xhr.readyState==4 && xhr.status==200){ //判断状态,xhr请求状态为4,表示接收响应数据成功;当status的值是200的时候,表示服务器已经正确的处理请求以及给出响应 $("uname-show").innerHTML=xhr.responseText; //提示内容 }; }; //4.发送请求 xhr.send(null); //get请求,参数写null } //2.定义函数,判断两次密码是否一致 //当确认密码框失去焦点时,验证两次输入的密码是否一致,并给出提示(通过/密码不一致) //给upwd 和 cpwd 加id function check_pwd(){ //1.获取两个密码框的值 var upwd=$("upwd").value; //$("upwd") 获取id值,函数在common.js中封装 var cpwd=$("cpwd").value; if(upwd==cpwd && upwd!=""){ //判断是否相等,且密码不为空 $("pwd-show").innerHTML="通过"; //提示到span中,用innerHTML }else{ $("pwd-show").innerHTML="两次密码输入不一致"; }; }; </script>
(5) PHP code to verify whether the user name is repeated
Function: Accept the uname value from the front end, query whether the same name exists in the database, and Give the return prompt
The code is as follows:
<?php //data/users/check-name.php #1.连接数据库 require("../init.php"); #2.接收前端传过来的uname $uname=$_REQUEST["uname"]; #3.拼接sql,并查询uname是否存在 $sql="SELECT * FROM xxx_user uname='$uname'"; $result=mysqli_query($conn,$sql); #4.根据查询的结果输出相应 $row=mysqli_fetch_row($result); //抓取一条数据,即当前uname对应的数据 if($row==null){ //如果$row为空,即数据库中没有相同的用户名存在 echo "通过"; }else{ echo "用户名称已存在"; };
Thank you for reading, I hope you can benefit from reading the code.
This article is reproduced from: https://blog.csdn.net/weixin_38840741/article/details/80030792
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of PHP+javascript implements user registration module (source code attached). For more information, please follow other related articles on the PHP Chinese website!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor