I am fairly new to programming, and was trying to save a JavaScript variable to an existing file:
<script src="./jquery.js"></script>
<script>
var data = inputs
function saveLists(){
$.ajax({
type: 'POST',
url: "save.php",
data: "data",
success: function(){
alert("success!")},
error: function(){
alert("Failed!")},
dataType: "text"
})
}
</script>
I define the inputs variable earlier, and I am using POST method and the following php server side:
<?php
$data = $_POST["data"];
$f = fopen('Inputs.txt', 'a+');
fwrite($f, $data);
fclose($f);
?>
When I call the saveLists() function the success alert comes up, but the data is not written to Inputs.txt. Any ideas on what I am doing wrong?
data: "data"so you are just sending the string "data" and PHP is complaining about an undefined index but you're ignoring it.dataneeds to be a query string e.g.data: 'data=something'or an object e.g.data: { data: 'something' }.success:then it's most likely the php is not handling it correctly.