Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 用户注册页面问题 求指点啊

用户注册页面问题 求指点啊

Jun 23, 2016 pm 01:59 PM

我用PHP AJAX 写了一个用户注册页面 如果用户名没有通过的话 不能跳到下一行输入密码 该怎么做?


回复讨论(解决方案)

代码? 代码?

代码? 代码? 我写的代码吗?就是一个注册页面 一个ajax.js 一个检查用户名是否存在的ckuser.php页面

目前是这样的 我想改成如果用户名没有通过,焦点一直在用户名的那个输入框 改怎么加代码?

是啊,不贴出代码,怎么知道问题出在哪?

目前是这样的 我想改成如果用户名没有通过,焦点一直在用户名的那个输入框 改怎么加代码?

没通过校验的话,在让用户名输入框获得焦点就行了

是啊,不贴出代码,怎么知道问题出在哪? 不是出现问题,是我不知道该怎么添加这个功能


目前是这样的 我想改成如果用户名没有通过,焦点一直在用户名的那个输入框 改怎么加代码?

没通过校验的话,在让用户名输入框获得焦点就行了 代码改怎么写啊?

添加也要在原来的代码上加,把你的代码贴出来吧。

添加也要在原来的代码上加,把你的代码贴出来吧。

reg.php<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link href="css/css.css" rel="stylesheet" type="text/css"><title>用户注册</title></head><body><script type="text/javascript" src="js/jquery-1.9.1.min.js"></script><script type="text/javascript" src="js/ajaxlist.js"></script><script>function check(){	checkUserName_1('username','16','unamespan')	}</script><table align="center">    	<tr>        	<td width="61" height="100"> </td>        </tr>    	<tr>        	<td>用户名:</td>            <td width="254"><input onblur="checkUserName_1('username','16','unamespan')" onfocus="onFouces('unamespan')" name=username keytype2="uname" class="input_txt" keytype="1" style="height:22px; width:222px" /><strong><font color="#FF0000">*</font> </strong></td>            <td width="426"><span class=gray id=unamespan>3-16个字符</span></td>        </tr>        <tr>        	<td>密 码:</td>            <td width="254"><input onblur="checkpw_1('password','6','upw')" onfocus="onFocus('upw')" class="input_txt" type=password keytype2="pw" name=password keytype="1" style="height:22px; width:222px" /><strong><font color="#FF0000">*</font> </strong></td>            <td width="426"><span class=gray id=upw>6-16个字符</span></td>        </tr>         <tr>        	<td>确认密码:</td>            <td width="254"><input onblur="checkSame_1('password','passsAgain','urpw')" onfocus="onFocus('urpw')" class="input_txt" type=password keytype2="rpw" name=passAgain keytype="1" style="height:22px; width:222px" /><strong><font color="#FF0000">*</font> </strong></td>            <td width="426"><span class=gray id=urpw>再次输入密码</span></td>        </tr>            </table></body></html>
Copy after login

添加也要在原来的代码上加,把你的代码贴出来吧。

ajaxlist.jsvar username_help='3-16个字符(a-z,0-9,_)'; var username_ok='<img  src="/static/imghw/default1.png"  data-src="js/jquery-1.9.1.min.js"  class="lazy"  src=img/OK.png align=absbottom alt="用户注册页面问题 求指点啊" >用户名可以使用'; var username_error='<img  src="/static/imghw/default1.png"  data-src="js/jquery-1.9.1.min.js"  class="lazy"  src=img/NO.png align=absbottom alt="用户注册页面问题 求指点啊" > 用户名超过了16个字符!'; var username_error_1='<img  src="/static/imghw/default1.png"  data-src="js/jquery-1.9.1.min.js"  class="lazy"  src=img/NO.png align=absbottom alt="用户注册页面问题 求指点啊" > 您的用户名输入格式不正确!'; var username_exist='<img  src="/static/imghw/default1.png"  data-src="js/jquery-1.9.1.min.js"  class="lazy"  src=img/NO.png align=absbottom alt="用户注册页面问题 求指点啊" > 您填写的用户名已经存在!'; var username_null='<img  src="/static/imghw/default1.png"  data-src="js/jquery-1.9.1.min.js"  class="lazy"  src=img/NO.png align=absbottom alt="用户注册页面问题 求指点啊" > 用户名长度最少3位!'; function getHTTPObject(){	var xmlhttp = false; 	if(window.XMLHttpRequest){ 		xmlhttp = new XMLHttpRequest(); 		if(xmlhttp.overrideMimeType){ 			xmlhttp.overrideMimeType('text/xml'); 		} 	}else{		try{ 			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 		}catch(e){ 			try{ 				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 			}catch(E){ xmlhttp = false; } 		} 	} 	return xmlhttp; } var http = getHTTPObject(); function getAbsLeft(e){ 	var l=e.offsetLeft; 	while(e=e.offsetParent) l += e.offsetLeft; 	return l; } function getAbsTop(e) { 	var t=e.offsetTop; 	while(e=e.offsetParent) t += e.offsetTop; 	t=t+18; 	return t; } //...............................................................function onFouces(str){	document.getElementById(str).className='word_help';	if(str=='unamespan'){		document.getElementById(str).innerHTML=username_help;	}}function backState(str){	if(str=='uname'){		document.getElementById('username').keytype=0;		document.getElementById('unamespan').className='gray';		document.getElementById('unamespan').innerHTML=username_ok;	}}function checkUserName_1(obj_str,num,obj_strl){	var obj=document.getElementById(obj_str);	var obj_help=document.getElementById(obj_strl);	obj.keytype=0;	if(obj.value.length<3){		obj.keytype=1;		obj_help.innerHTML=username_null;		obj_help.className=word_error;		return false;	}	if(obj.value.length>num){		obj.keytype=1;		obj_help.innerHTML=username_error;		obj_help.className=word_error;		return false;	}		//检查是否有不符合规定的字符 	var name_str=obj.value; 	//name_str=name_str.replace(/[\u4e00-\u9fa5]/g,''); 	name_str=name_str.replace(/[a-zA-Z0-9_]/g,''); 	name_str=name_str.replace(/-/g,''); 	if(name_str!=''){ 		obj.keytype=1; 		obj_help.innerHTML=username_error_1; 		obj_help.className=word_error; 		return false; 	} 		//检测用户名是否存在	var username = document.getElementById('username').value;	url = "checkuserreg.php?username="+username;	http.open("GET",url,true);	http.onreadystatechange = handleHttpResponseForUserName_1;	http.send(null);		if(obj.keytype==0){ 		backState("uname"); 	} }function handleHttpResponseForUserName_1(){ 	if(http.readyState == 4){ 		if (http.status == 200) { 		returnStr=Trim(http.responseText);  			if(returnStr=="1"){ 				var obj_1=document.getElementById("unamespan"); 				obj_1.innerHTML=username_exist; 				document.getElementById("username").keytype=1; 				obj_1.className=word_error; 				return false; 			} 		} 	} } function Trim(str) { 	return RTrim(LTrim(str)); } function LTrim(str) { 	return str.replace(/^[ \t\n\r]+/g, ""); } function RTrim(str) { 	return str.replace(/[ \t\n\r]+$/g, ""); } 
Copy after login

添加也要在原来的代码上加,把你的代码贴出来吧。

<?phpheader('ontent-Type:text/html;charset=GB2312');//避免输出乱码$dbhost     ="localhost";$dbuser     ="root";$dbpassword = "root";mysql_connect($dbhost,$dbuser,$dbpassword) or die("error!");mysql_query("set names 'gbk'");mysql_select_db('db_text');$username=trim($_GET['username']);//获取注册名$sql="select username from tb_user where username='".$username."'";//查询会员名$result=mysql_query($sql);$num=mysql_num_rows($result);$rows=mysql_fetch_array($result);if($num<>0){          echo "1";}else{          echo "0";}mysql_close();//关闭数据库连接?>
Copy after login

给你个思路,如果焦点离开判断校验没通过的话,再获取焦点就行了。这样就没办法进行后面的输入了。

给你个思路,如果焦点离开判断校验没通过的话,再获取焦点就行了。这样就没办法进行后面的输入了。 我也是这样想的 可是我不知道该怎么写代码

你的username input 框没有id值,能运行成功吗?

if(obj.value.length<3){
obj.keytype=1;
obj_help.innerHTML=username_null;
obj_help.className=word_error;
obj.focus(); //加上这句,下面的验证类似。
return false;
}

function handleHttpResponseForUserName_1(){     if(http.readyState == 4){         if (http.status == 200) {         returnStr=Trim(http.responseText);              if(returnStr=="1"){                 var obj_1=document.getElementById("unamespan");                 obj_1.innerHTML=username_exist;                 document.getElementById("username").keytype=1;                 obj_1.className=word_error;                 return false;             } else {               document.getElementById("username").focus(); // 加上这个            }        }     } }
Copy after login
Copy after login

function handleHttpResponseForUserName_1(){     if(http.readyState == 4){         if (http.status == 200) {         returnStr=Trim(http.responseText);              if(returnStr=="1"){                 var obj_1=document.getElementById("unamespan");                 obj_1.innerHTML=username_exist;                 document.getElementById("username").keytype=1;                 obj_1.className=word_error;                 return false;             } else {               document.getElementById("username").focus(); // 加上这个            }        }     } }
Copy after login
Copy after login
这个起作用了 就是符合条件的通过不了,不符合的能通过

你的username input 框没有id值,能运行成功吗?

if(obj.value.length<3){
obj.keytype=1;
obj_help.innerHTML=username_null;
obj_help.className=word_error;
obj.focus(); //加上这句,下面的验证类似。
return false;
} 这个好像没有作用 楼下的有作用了

噢,你是不合法(用户已存在)时返回 1

function handleHttpResponseForUserName_1(){     if(http.readyState == 4){         if (http.status == 200) {         returnStr=Trim(http.responseText);              if(returnStr=="1"){                 var obj_1=document.getElementById("unamespan");                 obj_1.innerHTML=username_exist;                 document.getElementById("username").keytype=1;                 obj_1.className=word_error;                document.getElementById("username").focus(); // 加上这个                return false;             }        }     } }
Copy after login
Copy after login

噢,你是不合法(用户已存在)时返回 1

function handleHttpResponseForUserName_1(){     if(http.readyState == 4){         if (http.status == 200) {         returnStr=Trim(http.responseText);              if(returnStr=="1"){                 var obj_1=document.getElementById("unamespan");                 obj_1.innerHTML=username_exist;                 document.getElementById("username").keytype=1;                 obj_1.className=word_error;                document.getElementById("username").focus(); // 加上这个                return false;             }        }     } }
Copy after login
Copy after login
高手 放在里面不起作用了 怎么回事啊

在 returnStr=Trim(http.responseText); 后面加上
alert(returnStr + ':' + returnStr.length);
看看有什么

在 returnStr=Trim(http.responseText); 后面加上
alert(returnStr + ':' + returnStr.length);
看看有什么 0:1

那不是对的吗?
返回的是 0 就显示“用户名可以使用”

那不是对的吗?
返回的是 0 就显示“用户名可以使用” 是啊 可是刚才写的那段代码加在里面不起作用,加在外面完全又完全相反,高手兄,该怎么改啊

突然发现你有用 jquery


那么还要自己写那些做什么?
建议你改写成直接用 jquery 的,简单的例子

<script>$(function() {  $('#username').blur(function() {    $.post('check.php', {username:$(this).val()}, function(m) {      $('#unamespan').html(m == 1 ? '不可用' : '可用');      if(m == 1) $('#username').focus();    });  });});</script>数字打头不可用,字母打头可用<br>用户名:<input name=username id=username class="input_txt"  style="max-width:90%" /><span class=gray id=unamespan>3-16个字符</span><br>
Copy after login
check.php
<?phpecho is_numeric($_POST['username']{0}) ? 1 : 0;
Copy after login
Copy after login

是不是很简单呢?

突然发现你有用 jquery


那么还要自己写那些做什么?
建议你改写成直接用 jquery 的,简单的例子

<script>$(function() {  $('#username').blur(function() {    $.post('check.php', {username:$(this).val()}, function(m) {      $('#unamespan').html(m == 1 ? '不可用' : '可用');      if(m == 1) $('#username').focus();    });  });});</script>数字打头不可用,字母打头可用<br>用户名:<input name=username id=username class="input_txt"style="height:22px; width:222px" /><span class=gray id=unamespan>3-16个字符</span><br>
Copy after login
check.php
<?phpecho is_numeric($_POST['username']{0}) ? 1 : 0;
Copy after login
Copy after login

是不是很简单呢? OK 谢了 高手兄

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles