当前位置:首页 > php ajax 开发中的注意事项

php ajax 开发中的注意事项

点击次数:1700  更新日期:2011-01-04
\n

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


\n

1.注意几个编码地方


\n

1.1表单所在的网页的:meta


\n

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


\n

1.2XMLHTTPRequest GET的编码


\n

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


\n

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


\n

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


\n

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


\n

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


\n

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


\n

1.4数据连接的编码


\n

mysql_query(“SET CHARACTER SET UTF8″);


\n

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


\n

mysql_query(“SET CHARACTER SET GBK”);


\n

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


\n

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


\n

例如:


\n

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


\n

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


\n

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


\n

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


\n

表单页:


\n

3.1JS部分


\n

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);

}>}


\n

function processRequest() {


\n

var tran;


\n

if (httpRequest.readyState == 4 || httpRequest.readyState == “complete”) {


\n

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


\n

tran = httpRequest.responseText;


\n

//setGlobalValue(tran);


\n

alert(tran);


\n

} else {


\n

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


\n

}


\n

}


\n

}


\n

function sendRequest(strurl) {


\n

httpRequest = false;


\n


\n

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


\n

httpRequest = new XMLHttpRequest();


\n

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


\n

try {


\n

httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);


\n

} catch (e) {


\n

try {


\n

httpRequest = new ActiveXObject(“Microsoft.XMLHTTP”);


\n

} catch (e) {}


\n

}


\n

}


\n

if (!httpRequest) {


\n

window.alert(“通讯失败”);


\n

return false;


\n

}


\n

httpRequest.onreadystatechange = processRequest;


\n

httpRequest.open(“GET”, strurl, true);


\n

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


\n

httpRequest.send(null);


\n

}>}


\n

function asychronouscheck(strparam){

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

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

}

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

sendRequest(strurl);


\n

<BR pre=”">


\n

function asychronouscheck(strparam){


\n

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


\n

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


\n

}


\n

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


\n

sendRequest(strurl);
表单部分


\n

<dl>

<dt>新帐号</dt>

<dd><label id=”state3″></label></dd>

<dd><input type=”text” name=”nusername” id=”nusername” size=”26″ maxlength=”14″ onblur=”asychronouscheck(this)” /></dd>

</dl>


\n

<dl>


\n

<dt>新帐号</dt>


\n

<dd><label id=”state3″></label></dd>


\n

<dd><input type=”text” name=”nusername” id=”nusername” size=”26″ maxlength=”14″ onblur=”asychronouscheck(this)” /></dd>


\n

</dl>


\n

php文件


\n

<?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(emptyempty(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();

}

?>

来源:http://blog.csdn.net/xiaofanku

\n