Does javascript enhance specific encoding?

I am trying to use javascript without a frame (prototype, jQuery, etc.) to insert data passed from an html form into a mysql database. I managed to get this work, but, the problem is with some special characters that are commonly used in my language (æøå). utf-8 supports these characters, and everything works fine when I skip javascript. However, when I try to pass the contents of the html form via the javascript post function and executing the request with my working php script, each æøå is converted to à | øà ¥.

To break it, does javascript somehow force a specific encoding, unless otherwise specified in the code? I tried to specify charset in the header of the javascript and php file, as well as in the html form.

I believe this should be something wrong or missing in my javascript file, formpost.js:

    function sendText(visbool){
  var http =new GetXmlHttpObject();
  var url = "sendtext.php";
  var ovrskrift = document.form1.overskrift.value;
  var cont = document.form1.content.value;

  var params = "overskrift=" + ovrskrift + "&tekst=" + cont + "&visbool=" + visbool;
  http.open("POST", url, true);

  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");

  http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        // gjør noe smart her
    }
  }
  http.send(params);
}


function GetXmlHttpObject()
 { 
 var objXMLHttp=null;
 if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest();
  }
 else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 return objXMLHttp;
 }

sendtext.php:

<?php
//variabler for spørring
$date= date('Y-m-d');
$overskrift= $_POST["overskrift"];
$ostr='36';
$tekst= $_POST["content"];
//$tekst="test tekst";
$istr='32';
$bilde="";
$style="onlyinfo.css";
//$vis=$_POST['visbool'];
$vis=0;

require ('../config.php'); // henter informasjon om database tilkoblingen fra config.php

if (!$con){die('Could not connect: ' . mysql_error());} // lager feilmelding hvis tilkoblingen mislyktes

// spørring for databasen, konstrueres etter variablene som er angitt ovenfor
$sql="INSERT INTO tbl_info(dato,overskrift_tekst,overskrift_str,infotekst_tekst,infotekst_str,bilde,style,vismeg) VALUES('" . $date . "','" . $overskrift . "','" . $ostr . "','" . $tekst . "','" . $istr . "','" . $bilde . "','" . $style . "','" . $vis . "')";
$result = mysql_query($sql); // kjører spørring og lagrer resultat i $result

mysql_close($con); // lukker database tilkoblingen
?>

createdlide.php: (reserved .php extension for future code implementation)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<title>inforMe: Legg til side</title>
<link type="text/css" rel="stylesheet" href="./css/style.css" id="adminss">  
<script src="./js/formpost.js"></script>
<script type="text/javascript" src="./tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
    mode : "textareas"
});
</script>

</head>
<body>
<div class="imgupload">
  <input type="button" value="Last opp bilde"> <img src="" />
</div>
<div class="form">
  <form name="form1" method="post" action="sendtext.php">
    <label for="overskrift">Overskrift:</label>
    <input type="text" size="70" id="overskift" name="overskrift"><br />
    <label for="content">Tekst:</label>
    <textarea id="content" name="content" cols="50" rows="15">This is some content that will be editable with TinyMCE.</textarea>
    <input type="button" value="save draft" onclick="sendText(0)"/>
    <input type="button" value="save and publish" onclick="sendText(1)"/>
    <input type="submit" value="regular submit button"/>
  </form>
</div>

</body>
</html>

NB! I really don't know where to post this information. Ok .. I decided to go with this here: First of all, thanks for all the good suggestions below. I believe that this question has been answered. As for my problem, it is only partially resolved.

It looks like utf-8 encoding is lost somewhere along the line here, if I create a new mysql database with Latin-1 encoding and change the charset parameters in my files to iso-8859-1, it works exactly. Since my test setup is based on any Norwegian software (browsers, db server, etc.), I suppose, wherever it is indicated otherwise, it will return iso-8859-1

NBB! . , :

header ('Content-Type: text/plain; charset = utf-8');

sendtext.php.

+5
7

:

"application/x-www-form-urlencoded; charset=utf-8;"

, , utf-8 :

<META http-equiv="Content-Type" content="text/html; charset=utf-8" />

, encodeURI () , URL.

+7

JavaScript Unicode, . : Unicode!= UTF-8.

, . () Unicode, . , Unicode UTF-8.

.

+3

, , , PQW , , () , , ​​ ( , UTF-8 META ). Javascript ( , Javascript).

" " - , (, ), META- - .

+2

, HTML, , , , , ...

, HTML Javascript, UTF-8.

, PHP mb_internal_encoding...

+1

, encodeURIComponent.

var ovrskrift = encodeURIComponent(document.form1.overskrift.value);

escape. : escape(), encodeURI() encodeURIComponent().

:

[...] escape(), . encodeURIComponent(). [...]

... .

+1

( ) :

(1) JS charset:

<script src="file.js" type="text/javascript" charset="utf-8"></script>

(2) , - PHP, , , UTF8?:

<?php
$result = iconv("UTF-8","UTF-8//IGNORE", $result);
?>
+1

Javascript "" ​​ . , escape() unescape() ASCII.

F.

0
source

All Articles