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 .