SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Charset in ajax. How to send accented input to the server?

    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
    Code:
    <!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
    Code:
    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);
       http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
       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
    Code:
    <?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!

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this matter is interesting for someone in the file "server_script.php" I have set the charset to "utf-8". I think because Javascript treats every character as unicode, and iso-8859-1 isn't an unicode charset.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •