2012-01-21 15 views
0

この記事の著者がログインすると、すべての記事にボタン「編集」が表示されます。 著者が記事を表示すると、ボタンをクリックして編集を開始できます。AjaxとPHPエンコーディングの問題

"onclick"イベントの後、記事のIDがjavascriptに送信され、編集ボックスがajaxを使用して読み込まれ、別のフォーム入力とボタン「保存」が表示されます。

問題は、私のデータベースのエンコーディングが "UTF-8"であるということです。つまり、ユーザーが新しい記事を追加すると、その記事はウェブサイト上に正しく表示されます。

しかし、フォームのページを読み込んで編集しなければならないと、一度保存をクリックすると、彼はすべてのデータを失い、エンコードを変更します。

この

は、編集ボックスを表示されますするページボタン(ボックスをロードするボタン「編集」)AND div要素です:$ vが[0]の記事のIDです

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

。 REQは、クライアントが持っているブラウザに応じて、私のXMLHttpRequestやActiveXObjectのオブジェクトであることを心に留めておく

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*/ 


} 

これは、この記事のためにボックスをロードするJavaScript関数です。

AJAX機能はdiv要素の#editにこのリクエストの結果をロードします。

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

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

あなたのサーバーがutf-8ではなくiso-8859-1でエンコードされたデータを送信することは多分問題です。 データをjavascriptに送信する前にheader("Content-Type: text/html; charset=UTF-8");を送信してください。

+0

私はすでに試してみましたが、うまくいきません。うまくいきません。私はjavascriptのencoding..htmlエンコーディングをチェックしています。すべてのPHPファイルはUTF-8にあります。 – SmootQ

+0

データベースはUTF-8でエンコードされていますが、すでに関数を追加しています.. しかし更新中。エンコーディングの問題 – SmootQ

+0

@SimoTAQIエンコーディングしているブラウザで '' http://mywebsite.com/edit.php?n = '+ id +'&altr = '+ altr'を開くと?また、どのようにポストやいくつかのJavaScriptを介して、あなたのデータを戻って送信する、それは私には分かりません。 –