文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页网页设计PHP技巧 → php+ajax开发的注意事项

php+ajax开发的注意事项

时间:2010/2/4 11:12:00来源:本站整理作者:我要评论(0)

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

<BR pre=""> 

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" onblur="asychronouscheck(this)" /></dd> 

</dl> 

<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 

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



?>

 

相关视频

    没有数据

相关阅读 php输出内容乱码解决方法php批量获取首字母(汉字、数字、英文)我的php文件怎么打开_如何打开php文件的办法破解防盗链图片的php函数php显示错误信息方法如何在IIS7下设置支持PHP程序PHP技巧--通过COM使用ADODB成就PHP高手的五个必经之路

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 如何恢复Discuz!7.0被 CSS实现Tab技巧Linux配置DHCP服务器实例:linux配置教程如何在IIS7下设置支持PHP程序PHP技巧--通过COM使用ADODB

人气排行 dedecms数据库表和字段说明最小化数据传输——在客户端存储数据php如何自动跳转中英文页面如何在IIS7下设置支持PHP程序php批量获取首字母(汉字、数字、英文)经典php实现大文件上传源代码Windows环境PHP的session不能正常使用解决办PHP聊天室技术