php ajax 开发中的注意事项

最近在学php,由于项目的需要!想在php中用ajax来完成一些体验(减少业务处理的单页压力).发现最近也有一位朋友为此苦恼不已.不废话了!

1.注意几个编码地方

1.1表单所在的网页的:meta

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

1.2XMLHTTPRequest GET的编码

httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

此处设置不对!responseText会返回empty(没有内容),如果您有FireFox并装有FireBug组件的话,点击状态栏的绿色箭头打开控件面板(非OS的,FireBug的),选中Console会看到Response选项是:

Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (latin1_swedish_ci,COERCIBLE) for operation '=

当然如果是连接数据库的话也可能跟下面的(1.4)有关系.

1.3ajax GET请求的页面(.php)header

header("Content-Type:text/html;charset=UTF-8");

1.4数据连接的编码

mysql_query("SET CHARACTER SET UTF8");

如果你的数据库是GBK的或其它的字符集,为了统一编码还要与以上三个统一起来.下面我的示例用的数据库也是GBK,从昨天开始我一起把它设成:

mysql_query("SET CHARACTER SET GBK");

可还是有时发现会返回空(empty 我用的是ResponseText),千万不要写成UTF-8噢,数据库的字符集是没有中间的"-"

2.如果还是返回空或无效的值

例如:

a.html中有表单,a用XMLHTTPRequest和b.php通讯.

首先要保证b.php可以正确运行,例b.php?param=value打印出来的是你期望的值

如果a.html打印b.php返回的结果(ajax)与上面的(单独运行b.php)执行结果有出入.可以删除b.php中的空行试试!我想应该不会出现这种情况,但我有几次作demo删除后和删除前确实有出入

3.下面是朋友发给我的一个示例!我修改完的源码

表单页:

3.1JS部分

function processRequest() {

	var tran;

    if (httpRequest.readyState == 4 || httpRequest.readyState == "complete") { 

    	if (httpRequest.status == 200 || httpRequest.statusText == "OK") { 

        	tran = httpRequest.responseText;                

            //setGlobalValue(tran);

            alert(tran);

         } else { 

            alert("您所请求的页面发生错误!");

         }

    }

}

function sendRequest(strurl) {  

    httpRequest = false;

        

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...

    	httpRequest = new XMLHttpRequest();            

    } else if (window.ActiveXObject) { // IE

        try {

        	httpRequest = new ActiveXObject("Msxml2.XMLHTTP");

        } catch (e) {

            try {

            	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

            } catch (e) {}

        }

	}

   	if (!httpRequest) { 

        window.alert("通讯失败");

            return false;

    }

    httpRequest.onreadystatechange = processRequest;    

    httpRequest.open("GET", strurl, true);

    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

    httpRequest.send(null);

}>}
function asychronouscheck(strparam){    

    if(strparam.value.length == 0){

        document.getElementById("state3").innerHTML="新帐号不能为空";        

    }

    var strurl="checknewaccount.php?name="+encodeURIComponent(strparam.value);    

    sendRequest(strurl);

表单部分

<dl>

                <dt>新帐号</dt>

                <dd><label id="state3"></label></dd>

                <dd><input type="text" name="nusername" id="nusername" size="26" maxlength="14" οnblur="asychronouscheck(this)" /></dd>

</dl>       

php文件

<?php

header("Content-Type:text/html;charset=UTF-8");

$conn=mysql_connect('localhost','root','bus');

mysql_select_db('test',$conn);

mysql_query("SET CHARACTER SET UTF8");

$curName=GET['name'];

if(empty($curName)){

	echo "新帐号不能为空";

	exit();

}

$tSQL="SELECT uid FROM members WHERE username ='$curName'";

try{	

	$result=mysql_query($tSQL) OR die(mysql_error());

	$row = mysql_num_rows($result);

	flush();	 

	if($row>0){            

		echo "false";

	}else{

		echo "true";

	}      

    mysql_free_result($result);

	mysql_close($conn);

}catch(Exception $e){

	print $e->getMessage();

}

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值