php ajax 開發中的注意事項

來源:互聯網
上載者:User

最近在學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" onblur="asychronouscheck(this)" /></dd></dl>       

php檔案

<?phpheader("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();}?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.