您的位置:首页 > Web前端 > JQuery

jQuery+Ajax+PHP 制作简单的异步数据传输(测试用户名是否可用)

2014-11-02 14:36 751 查看
实现基本异步数据传输,略去与数据库交换,先直接在PHP端判断:用户名为 user1 即为不可用,

测试时外加了 普遍的 “Loading..." 功能,此功能可直接在PHP中循环延时 for($i=0;$i<10000000;$i++) 即可

最终效果如下:











代码如下,供日后回味参考...

PHP 部分 : 其中 user 为传送过来的元素

for($i=0;$i<10000000;$i++);
$user = $_GET['user'];
$str = "";
if($user == "user1")
$str .="User name already exists !";
else
$str .="User name is ok ~";

//echo json_encode($str) ;
echo ($str) ;


jQuery 部分:

<script language="javascript" src="jquery.min.js"  src="ajaxlib.js"src="ajaxgold.js"></script>
<script language="javascript" type="text/javascript">
$(function(){
$("#check").ajaxSend(function(){
$(this).html("<font style='background:#990000; color:#ffffff;'> Loading...</font>");
});
});
function startCheck(oInput){
if(!oInput.value){
oInput.focus();
$("#check").html("User name can not be empty !");
return;
}
/*    $.ajax({
url: 'config.php',
type: 'GET',
dataType: 'json',
data: oInput.value,
success: showResult
});
*/
$.get("config.php",
{user: oInput.value },
function(data){
showResult(data);
}
);
}
function showResult(oText){
var oSpan = document.getElementById("check");
oSpan.innerHTML = oText;
if(oText.indexOf("exists")>=0)
oSpan.style.color = "red";
else
oSpan.style.color = "green";
}

</script>


CSS 样式设置部分:

<style type="text/css">
body{
padding-left:200px;
}
</style>
</head>
<body>
<form name="form1"><table>
<tr>
<td>用户名:</td>
<td  colspan="2"><input type="text" name="User" onblur="startCheck(this)"/></td>
<td><span id="check"></span></td>
</tr>
<tr>
<td>输入密码:</td>
<td colspan="2"><input type="password" name="passw1"/></td><td></td>
</tr>
<tr>
<td>重复密码:</td>
<td colspan="2"><input type="password" name="passw2"/></td><td></td>
</tr>
<tr>
<td></td><td><input type="submit"value="注册"/></td>
<td><input type="reset" value="重置"/></td><td></td>
</tr>
</table></form>
</body>


学了数据库再慢慢从中搜索...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐