日期:2014-05-16 浏览次数:20774 次
学习HTML的几种触发
onblur 事件会在对象失去焦点时发生。onmouseup 事件会在鼠标按键被松开时发生。
最终要实现的效果是:
index.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" /> <title>无标题文档</title> <script type="text/javascript" src="ajax.js"></script> </head> <body> <form name="myform" action="" method="post"> 用户名: <input type="text" name="user" value="" onblur="funphp100()" /> <div id="php100"></div> </form> </body> </html>
// JavaScript Document var xmlHttp; function S_xmlhttprequest(){ if(window.XMLHttpRequest){ //Mozilla浏览器 xmlHttp=new XMLHttpRequest(); }else{ //IE浏览器 if(window.ActiveXObject){ try{ xmlHttp=new ActionXObject("Msxm12.XMLHTTP"); }catch(e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ } } } } } function funphp100(){ var f = document.myform.user.value; S_xmlhttprequest(); xmlHttp.onreadystatechange = byphp; xmlHttp.open("GET","for.php?id="+f,true); xmlHttp.send(null); } function byphp(){ if(xmlHttp.readyState == 1){ document.getElementById('php100').innerHTML = "<img src = loading.gif>"; } if(xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ var byphp100 = xmlHttp.responseText; document.getElementById('php100').innerHTML = byphp100; } } }
<?php if($_GET[id]){ sleep(1); $conn = mysql_connect('localhost','root',''); mysql_select_db('test',$conn); $sql = "select * from `user` where `name` = '$_GET[id]'"; $q = mysql_query($sql); if(is_array(mysql_fetch_row($q))){ echo "用户名已经存在"; } else echo "可以使用"; } ?>