Hi,
Please can you tell me why when I try to send an “à” (a grave) from an html input textbox to the server, I receive an “Ô (capital a-tilde)? I use ISO-8859-1 and an ajax get request. I put that charset both in html file and in php file but the bad result remains.
I have tried also to insert the charset in ajax code (see red line) but without success.
Can you help me please? Here is the code:
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<link rel="shortcut icon" href="include/favicon.ico">
<link rel="stylesheet" href="include/layout.css" type="text/css">
<script type="text/javascript" src="ajax_script.js"></script>
<title></title>
</head>
<body>
<form action='javascript:send_request()' method='get'>
<input type='submit' id='cmd_submit' name='cmd_submit' value='OK'>
</form>
<div id='box'>
<p id='load_box'></p>
<input type='text' name='txt_input' id='txt_input' value='à'>
</div>
</body>
</html>
ajax_script.js
var http = createObject();
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer") {
request_type = new ActiveXObject("Microsoft.XMLHTTP");
} else {
request_type = new XMLHttpRequest();
}
return request_type;
}
function send_request() {
document.getElementById('load_box').innerHTML = "Loading...";
var txt_input = encodeURI(document.getElementById('txt_input').value);
var cmd_submit = encodeURI(document.getElementById('cmd_submit').value);
nocache = Math.random();
http.open('get', 'server_script.php?txt_input=' + txt_input
+ '&cmd_submit=' + cmd_submit
+ '&nocache=' + nocache);
[COLOR="Red"] http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");[/COLOR]
http.onreadystatechange = show_response;
http.send(null);
}
function show_response() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('box').innerHTML = response;
}
}
server_script.php
<?php
header('Content-Type: text/html; charset=ISO-8859-1');
if ( (isset($_GET['cmd_submit'])) && ($_GET['cmd_submit'] == 'OK') ) {
$txt_input = $_GET['txt_input'];
echo "server response: $txt_input";
}
?>
I have tried many times with no success. I hope you can help me!
thanks!