2012-01-21 111 views
0

我在每篇文章中都有一個「編輯」按鈕,一旦這篇文章的作者登錄了。 當作者瀏覽他的文章時,他可以點擊按鈕開始編輯。Ajax和PHP編碼問題

經過一個「onclick」事件...文章的ID將被髮送到javascript,並且編輯框將使用ajax加載,使用另一個表單輸入和按鈕「保存」。

問題是,我的數據庫編碼是「UTF-8」...這意味着當用戶添加一篇新文章時,文章將在網站上正確顯示。

但是,一旦他必須編輯加載的表單頁面,並且一旦他點擊保存,他將會丟失所有數據並更改編碼。

這是頁面按鈕(按鈕「編輯」,其加載框),股利,其中將出現在編輯框:

echo ' 
<div id="edit"></div> 
<input type="button" class="btn" id="edt'.$v[0].'" onclick="editme('.$v[0].');" 
value="Edit">'; 

其中$ V [0]是文章的ID 。

這是一個將載入箱這篇文章的javascript函數:

function editme(id){   /*function editme with the Article id as parameter*/ 
var req=new getXHR();  /* creation of xmlhttprequest object*/ 
var altr=Math.random();  /* a random to avoid URL caching in some browsers*/ 
var url='http://mywebsite.com/edit.php?n='+id+'&altr='+altr; /*the requesting URL*/ 
req.open('GET',url,true);  //open the requesting url using GET method syncronously 
req.onreadystatechange=function(){ 
if (req.readyState==4){ /*readyState*/ 
if (req.status==200){  /*status ==200*/ 
    document.getElementById("edit").innerHTML=req.responseText; /*return response as an html form for editing*/ 
} 
} 
else{ 
document.getElementById("edit").innerHTML="Loading..."; /***wait loading message*/ 
} 
}; 
req.send(null); /*paramerters sending is null, cuz we have GET method*/ 


} 

記住,REQ是根據瀏覽器客戶端有我的XMLHttpRequest或ActiveXObject的對象。

的AJAX功能加載這個請求的結果到DIV #edit:

'http://mywebsite.com/edit.php?n='+id+'&altr='+altr; 

PHP頁面是edit.php,這裏是它的源代碼:

<?php 
include 'global/config.php'; //configuration and initialization of the constants 
include 'global/connection.inc.php'; // connection settings 
include MODELS_DIR.'disp.php'; // here I wrote display functions 
include MODELS_DIR.'update.php'; // here are all updating functions 
if (isset($_GET['edit']) && !empty($_GET['edit']) && isset($_GET['n']) 
&& !empty($_GET['n'])){ //test if there's the number of the article in the URL..etc 
$update=new update(); // update object 
$redirect=$update->updateArticle($_GET['n'],$_GET['pv']); //update article and return redirection string to redirect to a new page 
echo '<script>window.location.href="'.$redirect.'";</script>'; //redirection 
} 
$objet=new disp();  // display the whole fields of the article in form 
$annonce=$objet->viewItem($pdo->quote($_GET['n']),'Articles_tbl','id_annonce','');//view article in a form (editing mode) 


include VIEWS_DIR.'edit.php'; //the form of the editing in the views directory 

一旦AJAX發送數文章的edit.php控制器,它加載更新函數(update.php)和顯示函數(disp.php)...並加載窗體在視圖中...一旦編輯完成後,它重定向用戶到編輯文章的頁面。

問題是,所有的文件都有編碼「UTF-8」...但我仍然有編碼法國和阿拉伯字符的問題。

預先感謝您。

回答

1

我不知道,但也許問題是您的服務器發送數據編碼iso-8859-1而不是utf-8。 嘗試發送header("Content-Type: text/html; charset=UTF-8");,然後再將數據發送給javascript。

+0

我已經試過了,但徒勞無功..它不起作用。我已檢查了javascript編碼..html編碼..和所有php文件都是UTF-8 – SmootQ

+0

數據庫編碼爲UTF-8,並且它已經與添加函數配合使用.. 但是在更新..我注意到編碼問題 – SmootQ

+0

@SimoTAQI如果在瀏覽器中打開''http://mywebsite.com/edit.php?n ='+ id +'&altr ='+ altr',那麼編碼是?另外,你如何通過發佈或一些JavaScript發送你的數據,這是不明確的。 –