problem with jquery $.ajax and french characters when inserted in database
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(){
 $('#submi开发者_JAVA技巧t_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
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); ?>
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;
 加载中,请稍侯......
      
精彩评论