I'm having a little problem that's getting on my nerves, I couldn't find a solution, I also tryed googling it and I found nothing... (my field of expertise is in AS 2 and 3, but I still lack some JavaScript solid knowdlege)
The problem is that when I try to send a form's content with Ajax (I'm using the prototype library), for some reason the latin characters (accents and stuff, like áéíóú) turn a mess when I try to store them in a db, but not when I only display them with PHP. I tryed then to see if the problem was with the Ajax part, so I got rid of it and made a common submit with the form. That way worked perfectly, so this is why I think that the problem is there.
A phrase like this:
Probando acentos á é í ó ú
(That means "Trying accents á é í ó ú)
Ends up like this:
Probando acentos á é Ã* ó ú
May be is when I make the query string with the Form.serialize method. I don't know... I tryed using the decodeURI() with the query -decodeURI(Form.serialize)-, and it actually got better, but still not working.
The code so far is this (I will post only the vital code):
The form:
[HTML]
<form method="post" action="actions.php" id="post_form">
<p>Título:<br /><input type="text" name="postTitle" id="postTitle" style="width:100%" /></p>
<script type="text/javascript">
//<![CDATA[
var rte1 = new RichEditor("postText","100%",400,"<?=$_POST['postText']?>");
rte1.write('targetDiv');
//]]>
</script>
<input type="button" name="save" value="Guardar y Seguir Editando" />
<input type="button" name="publish" value="Publicar" />
</form>
[/HTML]
The RichText class is a class I developed that makes a rich text editor, it works perfectly so the error can't be dragged from there. Also, the issue I'm getting is also happening in the title, and that is a common text input.
The script:
Expand|Select|Wrap|Line Numbers
- var msg = $('message');
- msg.style.display = 'none';
- var form = $('post_form');
- form.save.onclick = prepare;
- form.publish.onclick = prepare;
- function prepare(e) {
- var obj = e?e.target:window.event.srcElement;
- submitForm(obj.name);
- }
- function submitForm(method) {
- var params = decodeURI(Form.serialize(form));
- params += '&method=' + method;
- var req = new Ajax.Request('actions.php',{
- method:'post',
- parameters:params,
- onLoading:loading,
- onComplete:avisar
- });
- }
- function loading() {
- msg.style.display = 'block';
- msg.innerHTML = 'Guardando...';
- Form.disable(form);
- }
- function avisar(response) {
- Form.enable(form);
- msg.style.display = 'block';
- msg.innerHTML = response.responseText;
- }
[PHP]
include('config.php');
include('functions.php');
$section = $_POST['section'];
$method = $_POST['method'];
$postTitle = $_POST['postTitle'];
$postContent = $_POST['postText'];
if($section == "write_post") {
$sql = "INSERT INTO posts (
post_author,
post_date,
post_title,
post_content,
post_status,
post_comments,
post_category,
post_type
) ";
if($method == "save") {
$postStatus = 'draft';
} else if ($method == "publish") {
$postStatus = 'published';
}
$sql .= "VALUES(
1,
'',
'$postTitle',
'$postContent',
'$postStatus',
'open',
1,
'post'
)";
$dblink->createResultSet($sql, $db); //when inserting the data in the db all the latin characters are a hell of a mess
echo 'Tu post fue guardado con exito! ' . $postTitle; //but here here the $postTitle is displayed correctly
}
[/PHP]
($dblink is an instance of a class I programmed to manage db queries, it works alright, the problem only happens when I use the Ajax way to "submit" the form)
Also, my database has the latin1_general_ci collation, but I tried other variants and also UTF8 variants and all I got is a different mess each time, but a mess nontheless.... ^_^'
I'd really appreciate any help you can provide me.
Thanks a lot!
Best regards
The_Nephilim