I have recently started coding with javascript/php using the Cloud9 IDE.
I know this may be a duplicate of some questions on here, but I have yet to find a solution that works for me.
I have an array of custom javascript objects that I would like to save to a file on the server (later to be retrieved to repopulate data on the page):
<script>
var questionList = [];
function saveToFile() {
var x =JSON.stringify(questionList);
$.post("saveQuestion.php", {data : x}, function(){alert("File saved successfully")});
}
</script>
//in the body of the html
<button id="saveToFileButton" type="button" onclick='saveToFile()'>Save to file</button>
The php file 'saveQuestion.php' is in the same directory, and is as follows:
<?php
$data = $_POST['data'];
file_put_contents("savetest.txt", $data);
?>
However, despite my best efforts, 'savetest.txt' remains empty.
I know that 'x' is being assigned the correct value as I can print it out to a html element.
Edit: The php script has permission to write to the file, but as far as I can tell it is never called. What would be a possible cause/solution to this?
Can someone offer a simple explanation that highlights what I am doing wrong and possible ways to fix it?
Thanks!