search
HomeBackend DevelopmentPHP Tutorial用户注册页面问题 求指点啊

我用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>

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

ajaxlist.jsvar username_help='3-16个字符(a-z,0-9,_)'; var username_ok='<img  src="/static/imghwm/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/imghwm/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/imghwm/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/imghwm/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/imghwm/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, ""); } 

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

<?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();//关闭数据库连接?>

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

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

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

if(obj.value.length         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(); // 加上这个            }        }     } }

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(); // 加上这个            }        }     } }
这个起作用了 就是符合条件的通过不了,不符合的能通过

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

if(obj.value.length         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;             }        }     } }

噢,你是不合法(用户已存在)时返回 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;             }        }     } }
高手 放在里面不起作用了 怎么回事啊

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

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

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

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

突然发现你有用 jquery
<script></script>

那么还要自己写那些做什么?
建议你改写成直接用 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>
check.php
<?phpecho is_numeric($_POST['username']{0}) ? 1 : 0;

是不是很简单呢?

突然发现你有用 jquery
<script></script>

那么还要自己写那些做什么?
建议你改写成直接用 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>
check.php
<?phpecho is_numeric($_POST['username']{0}) ? 1 : 0;

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

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools