tags:

views:

41

answers:

2

hello I get french characters who look like this é but are é for example. I am using jquery ajax and making a form validation with php then inserting in the database, i have try many things but nothing seems to get rid of the problem and i know it's because of jquery ajax because if i make a simple insert in database using only PHP eveything works fine.

$(function(){
 $('#submit_maj_password_email').click(function(){
var _data= $(this.form).serialize()
//var encodedString = escape($("#email1").val());
 alert(_data)
  $.ajax({
        type: 'POST',
        url: 'valid.php?var=maj_password_email',
        beforeSend: function(){},
        data:_data,
        cache: false,
        success: function(html){
         $('div#error').html(html)

         }
     })
})

})

php file:

<?php
if($_POST and $_GET["var"]=="maj_password_email"){ 
extract($_POST);  //we automatically extract $ variables
...validation....
...insertion in db.....
if( $password!=="default"  and password($password)==true ){
    $query=mysql_query("UPDATE sdf_inemails SET emails_email='$email' , emails_password=md5('$password')  WHERE  emails_id = '".$_COOKIE['_ID']."'")or die(mysql_error());
//mail
  }
}
?>

nothing seems to work help. Regards

A: 

Setup the encoding type for your database tables and each fields which contains french data. Most probably ISO-8859-1 would be the correct encoding type for French.

EDIT

put an echo in server script to verify the data before saving. then you can find out where the problem starts.

<?php

$sql = "INSERT INTO {$TABLE} xxxxxx";
echo $sql;
Muneer
It stands to reason that nowadays, UTF-8 is really the correct encoding for every language.
Pekka
it's doesn look to be a problem with the DB as i said when not sending thru ajax french chars shows correctly, and the charset in the DB is the default wich is "latin swedish ci"
tada
@tada. You can put an echo in the server script before saving the records. So that you can verify data is in correct format.for example. echo $SQL;
Muneer
+2  A: 

Like Pekka said, AJAX requests serve UTF-8 data by default.

When you extract the data of the $_POST variable, you must use utf8_decode, like so:

$email = utf8_decode($_POST["txtEmail"]);

And then save it to your database.

You also can not forget to set the correctly headers in both of your PHP and HTML pages.

Example HTML:

<?xml version="1.0" encoding="ISO-8859-1"?>

Example PHP:

<?php header("Content-Type: text/html; charset=ISO-8859-1",true); ?>
Philipe
thank you! i was doing so but badly, like this:$email=$_POST["email"];utf8_decode ( $email );
tada