weixin_33698043 2012-01-21 12:12 采纳率: 0%
浏览 28

Ajax和PHP编码问题

I have a button "edit" in every article, once the author of this article is logged in.. when the author views his article, he can click the button to start editing.

After a "onclick" event ... the ID of the article will be sent to javascript, and the editing Box will load using ajax, with another form inputs and button "save".

The problem is that , My database encoding is "UTF-8"... that means that when the user adds a new article, the article will display correctly on the website.

But once the page of the form in which he must edit loads , and once he clicks save , he will get all his data lost and changed encoding.

this is the page button (the button "edit" which loads the box) AND the div in which will appear the editing box:

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

where $v[0] is the ID of the article.

this is the javascript function that will load the box for this article:

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


}

keep in mind that req is my XMLhttpRequest or ActiveXObject Object according to the browser client has.

the ajax function loads this Request's result into the div #edit :

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

The PHP page is edit.php, here's its source code:

<?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

once the ajax sends the number of the article to the edit.php controller, it loads the update functions (update.php) and display functions(disp.php) ... and loads the form in the view ... once editing is finished ..it redirects the user to the page of the edited article.

The problem is that all the files have the encoding "UTF-8" ... but I still have problem with encoding french and arabic characters.

Thank you in advance .

  • 写回答

1条回答 默认 最新

  • 乱世@小熊 2012-01-21 12:45
    关注

    I'm not sure but maybe problem is that your server sends data encoded iso-8859-1 instead of utf-8. Try to send a header("Content-Type: text/html; charset=UTF-8"); before you send data to javascript.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大