Php+Ajax局部刷新验证实例
今天花费了一些时间,写了个Php+Ajax局部刷新验证实例,在此给大家共勉......
建立一个ajax.js文件,代码如下:
// JavaScript Document
var xmlHttp;
function S_xmlhttprequest()
{
xmlHttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getName(name)
{
if(name = document.myform.name.value)
{
S_xmlhttprequest();
xmlHttp.open("get","date.php?name="+name,true);
xmlHttp.onreadystatechange = byname;
xmlHttp.send(null);
}
}
function byname()
{
if(xmlHttp.readyState ==1)
{
document.getElementById('name').innerHTML = "<font color=red>loading....</font>";
}
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var name = xmlHttp.responseText;
document.getElementById('name').innerHTML = name;
}
}
}
function getEmail(email)
{
var email = document.myform.email.value;
if(email =="")
{
alert("用户名不能为空");
document.myform.email.focus();
return false;
}
else
{
S_xmlhttprequest();
xmlHttp.open("get","date.php?email="+email,true);
xmlHttp.onreadystatechange = byemail;
xmlHttp.send(null);
}
}
function byemail()
{
if(xmlHttp.readyState ==1)
{
document.getElementById('email').innerHTML = "<font color=red>loading....</font>";
}
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var email = xmlHttp.responseText;
document.getElementById('email').innerHTML = email;
}
}
}
建立一个register.php文件,代码如下:
<title>注册页面</title>
<script src="Ajax.js" language="javascript">
</script>
<body>
<form action="" method="post" name="myform">
<table align="center">
<tr><td>用户名:</td><td><input type="text" name="name" value="" onblur="getName('name')"/></td><td><div id="name"><font color="#CC66CC">*用户名必填*</font></div></td></tr>
<tr><td>邮箱:</td><td><input type="text" name="email" value="" onblur="getEmail('email')"/></td><td><div id="email"><font color="#CC66CC">*邮箱必填*</font></div></td></tr>
</table>
</form>
建立一个date.php页面,用于查询数据库 :
<?
sleep(1);
$connt = mysql_connect("localhost","root","123456");
mysql_select_db('test',$connt );
mysql_query("set names 'gb2312'");
if($_GET['name'])
{
$name = $_GET['name'];
$sql = "select * from test where name='$name'";
$restul = mysql_query($sql);
$array = mysql_fetch_row($restul);
// print_r($array);
if(is_array($array))
{
echo "<font color='red'>该用户名已经存在</font>";
}
else
{
echo "<font color='red'>该用户名可以用</font>";
}
}
if($_GET['email'])
{
$name = $_GET['email'];
$sql = "select * from test where email='$email'";
$restul = mysql_query($sql);
$array = mysql_fetch_row($restul);
// print_r($array);
if(is_array($array))
{
echo "<font color='red'>该邮箱已存在</font>";
}
else
{
echo "<font color='red'>该邮箱可以用</font>";
}
}
?>
此即可实现,注册的时候局部刷新的效果......
热门日志
分类
- Django(4)
- ssdb(1)
- Mac(7)
- C(1)
- memcache(1)
- Python(32)
- Vim(8)
- sed(2)
- ansible(3)
- awk(4)
- shell(3)
- about(1)
- git(9)
- bat(4)
- svn(0)
- docker(1)
- Tornado(1)
- go(2)
- 架构(18)
- Vue(1)
- game(2)
- Html(6)
- Java(8)
- Mysql(37)
- Ajax(2)
- Jsp(1)
- Struts(8)
- Linux(73)
- JavaScript(39)
- Staruml(0)
- Mouth(1)
- Php(102)
- Windows(8)
- Message(48)
- Lua(10)
- Compute(1)
- Redis(7)
- Nginx(12)
- Jquery(1)
- Apache(1)
- cocos2d-x(8)
最新日志
- DHCP(Dynamic Host Configuration Protocol) 动态主机配置协议
- 从技术专家到战略领袖:成就技术总监的路径与思维
- python 如何读取超大的文件
- python requests 模块
- 如何给自己充电?
- 告别2024,迎接2025:深耕梦想,向前而行
- linux 的 dns 缓存,NSCD 服务
- The following untracked working tree files would be overwritten by checkout
- insecure connection not allowed,产生原因及如何解决
- 逆境乐观,顺境警醒:生活的智慧与力量