ajax(一) 文本格式操作 get和post 实例

<html>
	<head>
		<title>用户注册</title>
		<meta http-equiv = "content-type" content = "text/html;charset=utf-8"/>
		<script type = "text/javascript" language = "javascript">
			//创建ajax引擎
			function getXmlHttpObject()
				{
				  var xmlHttp=null;
				  //不同的浏览器获取对象xmlhttprequest 对象方法不一样
				  try
				    {
				    // Firefox, Opera 8.0+, Safari
				    xmlHttp=new XMLHttpRequest();
				    }
				  catch (e)
				    {
				    // Internet Explorer
				    try
				      {
				      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				      }
				    catch (e)
				      {
				      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				      }
				    }
				  return xmlHttp;
				}
				
				var myXmlHttpRequest="";
				
				//验证用户名是否已经存在
				function checkName(){
					
					myXmlHttpRequest=getXmlHttpObject();
					//判断创建是否成功
					/*if(myXmlHttpRequest){
						//通过myXmlHttpRequest对象发送请求道服务器的某个页面
						//第一个参数表示请求的方式, get || post
						//第二个参数指定url,对哪个页面发出ajax请求(即http请求)
						//第三个参数 true表示异步机制 false表示不适用异步机制
						//使用myTime="+new Date()+"为了达到时时更新的效果 而不是从cache中取数据
						var url="/ajax/registerprocess.php?myTime="+new Date()+"&userName1="+$("userName").value;
						//window.alert(url);
						//打开请求
						myXmlHttpRequest.open("get",url,true);
						//指定回调函数  onreadystatechange状态改变事件触发器
						myXmlHttpRequest.onreadystatechange=chuli;
						//发送请求,如果是get请求 填写null即可
						myXmlHttpRequest.send(null);
					}*/
					//如果使用post方式提交则用下面的代码 
					//建议使用post方法 因为 已经解决了中文乱码问题,可以传送大量数据
					if(myXmlHttpRequest){
						
						var url="/ajax/registerprocess.php";
						var data1="userName1="+$("userName").value;
						
						//打开请求
						myXmlHttpRequest.open("post",url,true);
						//必须加的一句话
						myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
						//指定回调函数  onreadystatechange状态改变事件触发器
						myXmlHttpRequest.onreadystatechange=chuli;
						//发送请求,如果是get请求 填写null即可
						myXmlHttpRequest.send(data1);
					}
				}
				
				//回调函数
				function chuli(){
					//window.alert("处理函数被调用"+myXmlHttpRequest.readyState);//readyState 对象状态
					//取出从 服务器页面返回的数据
					if(myXmlHttpRequest.readyState==4){
						//取出值,根据返回信息的格式定 text || xml ||json
						//window.alert(myXmlHttpRequest.responseText);
						//window.alert("服务器返回"+myXmlHttpRequest.responseText);
						$("myres").value=myXmlHttpRequest.responseText;
					}
				}
				
				//封装一个方法
				function $(id){
					return document.getElementById(id);
				}
		</script>
	</head>
<body>
<form action="???" method="post">
	用户名字:<input type = "text" name = "userName" id = "userName">
	<input type = "button" onclick = "checkName()" value = "验证用户名">
	<input style = "border-width:0;color:red" type = "text" id = "myres"> 
	<br/>
	用户密码:<input type = "password" name = "password"><br>
	电子邮件:<input type = "text" name = "email"><br/>
	<input type = "submit" value = "用户注册">
</form>	

<form action="???" method="post">
	用户名字:<input type = "text" name = "userName2" id = "userName2">
	<input type = "button" value = "验证用户名">
	<input style = "border-width:0;color:red" type = "text" id = "myres2"> 
	<br/>
	用户密码:<input type = "password" name = "password"><br>
	电子邮件:<input type = "text" name = "email"><br/>
	<input type = "submit" value = "用户注册">
</form>	
</body>	
</html>



<?php
	
	//这两句话很重要,第一句告诉浏览器返回的数据是xml格式的
	//header("Content-Type: atext/xml;charset=utf-8");
	//告诉浏览器不要缓存数据
	//header("Cache-Control: no-cache");
	
	//接收数据 POST || GET 根据前面的请求方式而定
	$username=$_POST["userName1"];
	if($username=="zs"){
		echo '用户名被占用,对不起!';
	}else{
		echo '用户名可以用,恭喜!';
	}
	//echo  '用户名是'.$username;

	
?>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值