O have page with some javascript code and I sending data from textarea to php file save.php to save data on server
code:
var data = 'data='+document.getElementById("data").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
response.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","save.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(data);
send data is okey but if I echo data in php on save.php it's broken %ad -> �
save.php code:
$post_data = $_POST['data'];
echo $post_data;
textarea contains:
somethink like:
%packages --excludedocs
@^minimal
@core
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
with %addon is problem becouse it changed to �don com_redhat_kdump.....
That � is a special character which means an invalid character code was used. This is a sign that you have used an invalid encoding.
I would suggest you set the character encoding of your request in javascript.
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
Also don't echo the data directly into a web page from php without encoding it in HTML.
echo htmlentities($post_data, ENT_HTML5, 'UTF-8');
You can also look at the web request headers using your browsers development tools to verify that the server isn't sending the web page using a different character encoding. If it is you can set the header in PHP.
header('Content-Type: text/html; charset=utf-8');
Here index.php file
<!DOCTYPE html>
<html>
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<body>
<h2>KickStart Editace</h2>
<textarea name="text" id="data" style="margin: 0px; width: 800px; height:
600px;"></textarea><br>
<button type="button" onclick="loadDoc()">Načíst data</button>
<button id="save" onclick="save();return false;">Uložit</button>
<div>Stav dokumentu: <span id="demo"></span></div>
<div id="test" ></div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
$data = this.responseText;
document.getElementById("data").value=this.responseText;
document.getElementById("demo").innerHTML= '<h2 style="color:green;">Načteno</h2>';
}
};
xhttp.open("POST","load.php", true);
xhttp.send();
}
function save(){
var test=document.getElementById("test");
var response=document.getElementById("demo");
var data = 'data='+document.getElementById("data").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
response.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","save.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8;");
xmlhttp.send(data);
}
</script>
</body>
</html>
here is save.php
<?php
$post_data = $_POST['data'];
echo $post_data;
if (!empty($post_data)) {
$file = "/var/ftp/pub/scripts/installCentostest.cfg";
$fp = fopen($file, "w");
$data = str_replace("", "", $post_data);
fwrite($fp, $post_data);
fclose($fp);
echo '<h2 style="color:green;">Uloženo</h2>';
}
else
echo '<h1 style="color:red;">chyba!!! kontaktujte administrátora</h1>';
?>