For some reason, when I use fwrite to overwrite the contents of my file, it erases the data, but fails to write the new contents to it until fwrite is invoked a second time. Is there any way I can get around this?
My script uses HttpRequest to send a FormData object to an external script as if I had invoked a form submit, then the script saves the form data to a file on the server, and then uses another HttpRequest to get the responseText of the file on the server and apply it to the value of another element:
PHP.php
data = $_POST["name"];
fopen(filename, w);
fwrite(file, data);
fclose(file);
CORE.html
<script>
function functionName() {
obj = new XMLHttpRequest;
form = new FormData;
form.append("name", object.getElementById(elementId).value);
obj.open(POST, PHP.php, false);
obj.send(form);
obj2 = new XMLHttpRequest;
obj2.onreadystatechange = function() { if (obj2.readyState == 4 && obj2.status == 200) document.getElementById(elementId2).value = obj2.responseText };
obj2.open(POST, OUTPUT.txt, false);
obj2.send();
}
</script>
<textarea id=elementId onkeypress=functionName()>
</textarea>
<br>
<textarea id=elementId2>
</textarea>
OUTPUT.text
epty text document