2012-01-21 109 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发送你的数据,这是不明确的。 –