Blogger Information
Blog 44
fans 0
comment 1
visits 31057
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4月10日作业—$.ajax方法
时光记忆的博客
Original
618 people have browsed it
//html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>3.$.ajax() 无刷新验证</title>
</head>
<body>
<h2>用户登录</h2>
<form action="">
<fieldset>
<p>用户名:<input type="text" name="name"></p>
<!-- <span style="color:red;">用户名已存在不能注册</span> -->
</fieldset>
</form>
</body>
</html>
<script src='../js/jquery-3.3.1.js'></script>
<script>
//当失去焦点的时候进行验证
	$(':input').blur(function(){
	$.ajax({
			url:'api/demo1.php',
			type:'GET',
			dataType:'json',
			data: $('form:first').serialize()   //serialize()结果是一个字符串
			// data: $('form:first').serializeArray(),  //json
		}).done(function(res){ //成功回调
				console.log('res')
				if(res.status == 0){
					$('p').append($(res.tips))
				}else{
					$('p').append($(res.tips)).append($('<p>').css('color', 'lightskyblue').text('正在跳转。。。'))
				}
		})
	})
</script>
<--demo1.php-->
<?php
	// print_r($_GET);exit;
	//用数组来模拟数据库中已经存在的用户名,这些用户名是
	$nameList = ['admin', 'peter', 'php'];

	//当前提交的用户名
	$userName = $_GET['name'];

	//判断用户名是不为空
	if(strlen(trim($userName)) == 0){
		$status = 0;
		$tips = '<span style="color:red;">用户名不能为空</span>';
		// echo '<span style="color:red;">用户名不能为空</span>';
	}else if(is_numeric($userName)){
		$status = 0;
		$tips = '<span style="color:red;">用户名不能为纯数字</span>';
	}else if(in_array($userName, $nameList)){
		$status = 0;
		$tips = '<span style="color:red;">用户名太抢手了,换一个</span>';
	}else{
		$status = 1;
		$tips = '<span style="color:green;">恭喜,用户名可用</span>';
	}
		echo json_encode(['status'=>$status, 'tips'=>$tips]);

511858235757225621.jpg

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post