1

I have a php code that is catching a variable from a URL parameter and passing it over trough a html edditbale text field over to Jquery/Ajax and then further to an external php code that is passing it over to my Firebird DB. The problem is that jquery/ajax is changeing the characters like ä into ä and so on.

First i thought i have to encode the firebird SQl update with ISO8859_1 (which the table is using) bit then i found out its the Jquery which is changing the characters.

Here is my code:

?>
    <div id="wrap">
        <h3>Comment</h3>
        <div id="status"></div>
        <div id="content">
        <div id="editable" contentEditable="true">
<?php
        echo $row[21];
        ?>
</div>  
        <button id="save">Save</button>
        </div>  
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
  <script>
    $(document).ready(function() {  
        $("#save").click(function (e) {         
            var content = $('#editable').html()
            var nr = <?php echo $s; ?>; 
            $.ajax({
                url: 'save.php',
                type: 'POST',
                data: {
                content: content,
                nr: nr
                },      
                success:function (data) {               
                    if (data == '1')
                    {
                        $("#status")
                        .addClass("success")
                        .html("Data saved successfully")
                        .fadeIn('fast')
                        .delay(3000)
                        .fadeOut('slow');   
                    }
                    else
                    {
                        $("#status")
                        .addClass("error")
                        .html("An error occured, the data could not be saved")
                        .fadeIn('fast')
                        .delay(3000)
                        .fadeOut('slow');   
                    }
                }
            });     
        });     
        $("#editable").click(function (e) {
            $("#save").show();
            e.stopPropagation();
        });
        $(document).click(function() {
            $("#save").hide();  
        });
    });
</script>
Michael Müller
  • 371
  • 6
  • 23
  • I did it my way: just ad this to the last variable which you want to get submittetd to your database: iconv("ISO-8859-1", "UTF-8", $content); which is leaving open the last step: how so submit it in charset ISO-8859-1 ??? – Michael Müller Oct 14 '12 at 22:45

5 Answers5

1

i solved this by simply using a php translation before send the value to the db:

$content2 = utf8_decode($content);

Michael Müller
  • 371
  • 6
  • 23
0

Reading the jQuery.ajax documentation;

Default [content-type] is "application/x-www-form-urlencoded; charset=UTF-8"

In other words, what you're getting posted to your PHP page is an UTF-8 encoded string. If you want anything else (like ISO-8859-1), you'll need to convert it.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • how do i change that? i allready tried several thing, with no luck – Michael Müller Oct 14 '12 at 18:01
  • @MichaelMüller As far as I know (and I'm sure I'll be proven wrong if I am :) ), you can't (portably) change the charset `jQuery.ajax()` uses in posts, you will have to convert it server side. – Joachim Isaksson Oct 14 '12 at 18:06
  • Btw. i also have the Problem that it posts linebreaks as
    which should be first of all
    but actually real line breaks in my firebird db, how to solve this too?
    – Michael Müller Oct 14 '12 at 18:39
0

Try it

$.ajax({
        data: parameters,
        type: "POST",
        url: ajax_url,
        timeout: 20000,
        contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
        dataType: 'json',
        success: callback
});

You would also have to specify the charset on the server.

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>

Update answer again after your comment

See below URL I think it very important for you.

How do I transcode a javascript string to iso-8859-1

How do I transcode a Javascript string to ISO-8859-1?

Community
  • 1
  • 1
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • that did not work for me, i did put in the content type string unter the post line but where do i put the php charset? THis is the line that is passing the text with the wrong charsets: var content = $('#editable').html() – Michael Müller Oct 14 '12 at 18:16
  • I have update my answer. i have mention one URL. Please take a look. – Abid Hussain Oct 14 '12 at 18:24
  • I read that before and did not get any further. The problem is i have a very small basic understanding of php and sql but absolutly no jqery skills at all. – Michael Müller Oct 14 '12 at 18:30
  • sorry, i have no chance do understand this. I tried endless things but until i understand the java synthax i have no chance to translate those examples to mine. – Michael Müller Oct 14 '12 at 19:57
0

As said Joachim Isaksson, you can't submit data in another charset than UTF-8.

I usually use this function in my front controller to convert all $_POST array.

<?php
public function direct($POST)
{
          if (!empty($_POST)) {

            $flag_unicoded = false;

            if (strpos(strtolower($_SERVER['CONTENT_TYPE']), 'charset=utf-8') !== false) $flag_unicoded = true;
            if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && strpos(strtolower($_SERVER['CONTENT_TYPE']), 'charset') === false) $flag_unicoded = true;

            if ($flag_unicoded) {
              function utf8_decode_recursive($input) {
                $return = array();
                foreach ($input as $key => $val) {
                  if (is_array($val)) $return[$key] = utf8_decode_recursive($val);
                  else $return[$key] = utf8_decode($val);
                }
                return $return;          
              }
              $_POST = utf8_decode_recursive($_POST);
            }
            return $_POST;
          }
}
?>

EDIT

Not tested, but you could try this :

jQuery.ajaxSetup({'beforeSend' : function(xhr) {xhr.overrideMimeType('charset=ISO-8859-1'); }});
sdespont
  • 13,915
  • 9
  • 56
  • 97
0

I hope this doesn't appear as a duplicate answer. I had this problem with a jsp application. The page was in iso-8859-1, but using the jquery.serialize(), the data become broken when posted from IE. I saw Abid Hussain's suggestion above, but changed it to

contentType: "application/x-www-form-urlencoded;charset=UTF-8",

eg. telling the server that it was UTF-8 coming, not iso-8859-1 This solved my problem.

 $("#caleditform").submit(function(){
 $.ajax(
 {
 url:$("#caleditform").attr('action'),
 type: "POST",
 contentType: "application/x-www-form-urlencoded;charset=UTF-8",
 data: $('#caleditform').serialize(),
 success:function(html){
   $('#projectdialog').html(html);
 }
 }
 );
 return false;

});

Thank you

Bjarne Havnen
  • 91
  • 1
  • 7