ajax实现注册页面动态验证用户名是否已注册,不必提交即可验证。

今天学了一下ajax,感觉很爽啊。ajax真是很强大、

我首先就把我之前一直没解决的问题:如何在前台动态验证用户名是否已注册,而不必提交刷新之后再验证,上代码:

首先,jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <input type="text" name="text1" id="text1id" οnblur="Ajaxtest1();"><br> <input type="text" name="text2" id="text2id" /><br> <%-- <input type="button" value="button" οnclick="Ajaxtest1();"><br>--%> <div id="div1"></div> </body> </html>
这个其实只是两个文本框,输入用、

下面是js代码。验证用的、

<script type="text/javascript"> var xmlhttprequest=null; function Ajaxtest1(){ if(window.ActiveXObject){//IE浏览器 xmlhttprequest=new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest){ xmlhttprequest=new XMLHttpRequest(); } if(null!=xmlhttprequest){ var text1=document.getElementById("text1id").value; var text2=document.getElementById("text2id").value; xmlhttprequest.open("GET", "testajax1?t1="+text1+"&t2="+text2, true); xmlhttprequest.onreadystatechange=ajaxcallback; //var tt=document.getElementById("text1").innerHTML; xmlhttprequest.send(null); } } function ajaxcallback(){ if(xmlhttprequest.readyState==4){ if(xmlhttprequest.status==200){ var text=xmlhttprequest.responseText; document.getElementById("div1").innerHTML=text; } } } </script>这个没什么可说的,唯一注意的一点就是xmlhttprequest.open("GET", "testajax1?t1="+text1+"&t2="+text2, true);

这里面的testajax1是servlet,注意这个地址需和web.xml保持一致。

接下来是testajax1.java

package com.guang.ajax; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.guang.sqlhelp.sqlhelp; import java.sql.*; public class testajax1 extends HttpServlet { public testajax1() { super(); } int i=0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gbk"); response.setContentType("text/html;charset=gbk"); PrintWriter out = response.getWriter(); //System.out.println("doGet"+i); i++; String t1=request.getParameter("t1"); //System.out.println(t1); //out.println("Hello World"); sqlhelp sp=new sqlhelp(); Connection conn=sp.getconn(); Statement sta=sp.getsta2(conn); String sql="select * from baseinfo where usernum='"+t1+"'"; ResultSet set=sp.getset(sta, sql); int j=0; if(set!=null){ try { set.last(); } catch (SQLException e) { e.printStackTrace(); } try { j=set.getRow(); } catch (SQLException e) { e.printStackTrace(); } if(j==1){ out.println("此学号已被注册,请换一个!!"); } else{ out.println("恭喜您,这个可以注册!"); } out.close(); } } }
那个。上面的也很简单,就是一个取数据的过程,唯一的一个就是验证行数。这个也可以其他的方法。

sqlhelp在这里我就不贴出来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于 AJAX 和 PHP 的注册登录页面的简单实例: 1. HTML 页面 ```html <!DOCTYPE html> <html> <head> <title>AJAX PHP Register/Login Example</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> // 注册表单提交 $(document).on('submit', '#register-form', function(e){ e.preventDefault(); var username = $('#username').val(); var password = $('#password').val(); var confirmPassword = $('#confirm-password').val(); $.ajax({ type: 'POST', url: 'register.php', data: {username: username, password: password, confirm_password: confirmPassword}, dataType: 'json', success: function(response){ if(response.success){ alert('注册成功!'); }else{ alert(response.error); } } }); }); // 登录表单提交 $(document).on('submit', '#login-form', function(e){ e.preventDefault(); var username = $('#username').val(); var password = $('#password').val(); $.ajax({ type: 'POST', url: 'login.php', data: {username: username, password: password}, dataType: 'json', success: function(response){ if(response.success){ alert('登录成功!'); }else{ alert(response.error); } } }); }); // 用户名验证 $(document).on('blur', '#username', function(){ var username = $(this).val(); $.ajax({ type: 'POST', url: 'check_username.php', data: {username: username}, dataType: 'json', success: function(response){ if(!response.success){ alert(response.error); } } }); }); </script> </head> <body> <h2>注册</h2> <form id="register-form"> <div> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> </div> <div> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </div> <div> <label for="confirm-password">确认密码:</label> <input type="password" id="confirm-password" name="confirm_password" required> </div> <button type="submit">注册</button> </form> <h2>登录</h2> <form id="login-form"> <div> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> </div> <div> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </div> <button type="submit">登录</button> </form> </body> </html> ``` 2. PHP 后端代码 - `register.php` ```php <?php // 获取提交的数据 $username = $_POST['username']; $password = $_POST['password']; $confirmPassword = $_POST['confirm_password']; // 校验数据 if(empty($username) || empty($password) || empty($confirmPassword)){ die(json_encode(['success' => false, 'error' => '用户名或密码不能为空'])); } if($password !== $confirmPassword){ die(json_encode(['success' => false, 'error' => '两次输入的密码不一致'])); } // 此处省略数据库操作,假设注册成功 echo json_encode(['success' => true]); ``` - `login.php` ```php <?php // 获取提交的数据 $username = $_POST['username']; $password = $_POST['password']; // 此处省略数据库操作,假设登录成功 echo json_encode(['success' => true]); ``` - `check_username.php` ```php <?php // 获取提交的数据 $username = $_POST['username']; // 此处省略数据库操作,假设用户名已存在 echo json_encode(['success' => false, 'error' => '用户名已存在']); ``` 以上代码仅供参考,实际开发中需要根据具体需求进行适当修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值