I am trying to read contents of a local text file into text area, then modifying textarea and subsequently saving modified text area value into the same local file. I cannot use server side code so trying this with Jquery Ajax post method. My html looks like this -
<html>
<head>
<title>Edit Properties</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="./js/graph/graph.js"></script>
<script>
var testpath;
var buildpath;
var dataOnFile;
var buildnum;
function loadFile() {
var URL = "somepath";
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", URL, false);
xhttp.send("");
return xhttp.response;
}
function edit(){
$(document).ready(function() {
//console.log(loadFile());
$("#area").val(loadFile());//load the contents correctly
$("#save").click(function()
{
testpath = window.location;
buildpath=(testpath+"").replace("somepath1","");
buildpath= buildpath + "somepath2";
dataOnFile=$("#area").val();
console.log(dataOnFile);//logs updated value of text area
$.ajax({
type : "POST",
async:false,
data : dataOnFile,
url:buildpath,
dataType : "text",
success: function(data) {
alert("File Saved");
}
});
});
});
}
</script>
<body onload="edit()">
<p>
<textarea rows="50" cols="100" id="area"></textarea>
<input type='button' value='Save File' id="save"/>
</p>
</body>
</html>
This works without error but my changes are NOT saved into a file. Any pointers to resolve this?
Javascript was designed in a way that denies access to the file system. If you want to mannupilate files, you should probably use a server side language like php. This tutorial might help you get what your trying to do:
Beginners guide to Ajax with PHP