Okay, so here is the problem:
I have a form on my php page. When a user has entered a开发者_JS百科 name a presses submit a jQuery click event (on the submit button) collects then information and passes them on through $.ajax().
$.ajax({
url: "ajax/addGatheringSignup.php",
type: "POST",
async: true,
dataType: 'json',
data: {
"id": $_GET['id'],
"name": $signupNameInput.val()
},
success: function(jsonData){
if(jsonData[0].feedback == "ok"){
$signupForm = $('#singupform');
$signupForm.html('Signup successful!');
}else{
Alert(jsonData[0].feedback);
}
},
error: function(){
Alert("error alert");
}
});
As you can see the "name" field is the value from the name inputfield. But when i submit this to my php page (where I don't format anything within the text) its totally garbage in my MySql database. At the moment im trying to get the danish letters æ, ø and å to work.
Atm i know my mysql database are using UTF-8 and my meta-header for my index.php looks like this (every page is generated from the index.php page... ex index.php?page=random):
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
But nothings works. When i post: "æÆ-øØ-åÅ" to the database it saves as: "æÆ-øØ-åÅ". anyone know what i have to do?
EDIT 1:
I can see that on a successful ajax submit the html i set $signupForm to (line 13. in the code above) displays wrong as well (it's normally some danish words where I write the danish chars mentioned)
EDIT 2 (found one solution):
I found a way. $.ajax() according to the jQuery doc, always parses data as UTF8. I don't know why this messed up my code, but when i added *utf8_decode($name)* to the add-function it parsed correct (so i guess my charset must have been set to ISO-8859-1 hidden somehow?). This just made it easier since i could then turn my old charset ISO-8859-1 back on again and remove all my utf8_encode() functions.
My last problem was the one presentated in "EDIT 1". Here i found a solution on how to convert UTF8 strings (again because of $.ajax()):
function decode_utf8( s ){
return decodeURIComponent( escape( s ) );
}
The problem might be in your database connection. It's communicating in a given charset as well.
See mysql_set_charset()
.
The data in the Ajax-POST is UTF8-encoded, so in your PHP script (where you write to the database), you need to do the following:
$name = utf8_decode($_POST['name']);
That way you can store æÆ-øØ-åÅ
as æÆ-øØ-åÅ
instead of the ASCII malformed æÆ-øØ-åÅ
Also your database needs to be on the appropriate collation (for instance latin1_swedish_ci
)
I'm not sure but it sounds like you get mixed encoding types.
A page that is sent as UTF-8, but where input is transformed to ISO-8859-1 and store in a mysql UTF-8 encode table will fail.
You need to keep control on input ecodings type in relation to the type of encoding you have chosen to use as internal in php and external.
Try to see if this help you.
- PHP messing with HTML Charset Encoding
- http://www.tanzilo.com/2008/12/29/php-mysql-creating-a-website-in-your-local-language-smoothly/
function decode_utf8( s ){
return decodeURIComponent( escape( s ) );
}
is amazing a good short form is
Name = decodeURIComponent( escape( Name ) );
it will solve 'ISO 8859-1 Characters' problem in ajax response.
精彩评论