作者:_珊渣渣 | 来源:互联网 | 2013-08-08 17:38
最近在学php,由于项目的需要!想在php中用ajax来完成一些体验(减少业务处理的单页压力).发现最近也有一位朋友为此苦恼不已.不废话了!
1.注意几个编码地方
1.1表单所在的网页的:meta
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部分
[html] view plaincopyprint?
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 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);
}>}
[html] view plaincopyprint?
function asychronouscheck(strparam){
if( strparam.value.length == 0){
document.getElementById("state3") .innerHTML = "新帐号不能为空" ;
}
var strurl = "checknewaccount.php?name=" +encodeURIComponent(strparam.value);
sendRequest(strurl);
< BR pre = "" >
function asychronouscheck(strparam){
if(strparam.value.length == 0){
document.getElementById("state3").innerHTML="新帐号不能为空";
}
var strurl="checknewaccount.php?name="+encodeURIComponent(strparam.value);
sendRequest(strurl);
表单部分
[html] view plaincopyprint?
< 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 >
新帐号
php文件
[php] view plaincopyprint?
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();
}
?>