Sto cercando di utilizzare javascript, senza framework (prototipo, jQuery, ecc.) Per inserire i dati passati da un modulo html a un database mysql. Sono riuscito a farlo funzionare, ma ho avuto problemi con alcuni caratteri speciali comunemente usati nella mia lingua (æøå). utf-8 supporta questi caratteri e tutto funziona correttamente saltando il javascript. Tuttavia quando provo a passare il contenuto del modulo html tramite la funzione javascript post ed eseguo la query con il mio script php funzionante, ogni æøå viene convertito in Ã|ø¥.javascript forza un set di caratteri specifico?
Per scomporre, javascript forza in qualche modo un set di caratteri specifico quando nessun altro è determinato nel codice? Ho provato a specificare charset nell'intestazione del post javascript e il file php, nonché il modulo html.
credo che deve essere qualcosa di sbagliato o mancante nel mio file javascript, 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
?>
createslide.php: (estensione .php riservato per futura implementazione del codice)
<!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! Non so davvero dove mettere queste informazioni così .. beh .. Ho deciso di andare qui: Prima di tutto grazie per tutti i buoni suggerimenti qui sotto. Credo che la domanda abbia avuto risposta. Per quanto riguarda il mio problema, è solo parzialmente risolto.
Sembra che il set di caratteri utf-8 si perda da qualche parte lungo la linea qui, se creo un nuovo database mysql con il set di caratteri Latin-1 e cambi le opzioni di charset nei miei file su iso-8859-1 funziona perfettamente . Dal momento che la mia configurazione di test è basata su tutto il software norvegese (browser, server DB, ecc.), Suppongo che ovunque non sia specificato diversamente restituirà iso-8859-1
NBB! E ora va tutto bene di nuovo. tutto funziona correttamente quando aggiungo:
intestazione ('Content-Type: text/plain; charset = utf-8');
all'inizio del mio file sendtext.php.
Questa domanda è stata inviata molto tempo fa, ma devo dire questo: per favore dimmi che non è il tuo codice di gestione SQL, in quanto ti lascia completamente aperto per attacchi di SQL injection ... – Epcylon