I have the following code that displays information on a person in a table, and when a button labeled "learn" is clicked that information gets stored in a key/value type array that is then spliced into the specified JSON object.
All of this works just fine, but I want to alter the file that holds the JSON object itself, meaning I need to deal with server-side code. How can I take this insertion into my JSON object and actually change the textfile it is stored in (such that if I were to open it, the new information would be there)?
My code is as follows -->
index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="list.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title></title>
</head>
<body>
<table>
<tr>
<td>
<table>
<tr><td>Name</td><td class="name">Jenkins</td></tr>
<tr><td>Job</td><td class="job">Accountant</td></tr>
<tr><td>Size</td><td class="size">Small</td></tr>
</table>
</td>
<td class="description">average joe.</td>
<td>
<button class="learn">Learn</button>
</td>
</tr>
</table>
<script type="text/javascript">
$(".learn").click(function(){
// look at jsonData in list.js and append this element to the items list in the
// first element, at the first index without removing any content
jsonData[0].items.splice(0, 0, {
name: $(this).closest("table").find(".name").text(),
job: $(this).closest("table").find(".job").text(),
size: $(this).closest("table").find(".size").text(),
description: $(this).closest("td").siblings(".description").text()
});
// to show that it is properly appending the new information to the existing JSON object
console.log(jsonData);
});
</script>
</body>
</html>
list.js -->
var jsonData = [
{
name: "Friends",
items: [
{
name: "Steve",
job: "pro bowler",
size: "tall",
description: "Steve's my man!"
},
{
name: "Jessica",
job: "HR",
size: "average",
description: "a dear friend"
}
]
},
{
name: "Co-Workers",
items: [
{
name: "Martin",
job: "my boss",
size: "tall",
description: "I don't like him"
},
{
name: "Susan",
job: "Intern",
size: "average",
description: "It's not like I have a crush on her or anything..."
}
]
}
]
So again, it can be seen in the console that jsonData has the information for "jenkins" added to it; however, the textfile itself remains unchanged and I would like it to reflect this addition. Thank you.
It is not possible to change files on the local file system using JavaScript (at least not in all browsers). I would suggest something like:
JavaScript:
$(".learn").click(function(){
$.ajax({
url: "save.php?action=save",
method: "POST",
data: { elem: {
name: $(this).closest("table").find(".name").text(),
job: $(this).closest("table").find(".job").text(),
size: $(this).closest("table").find(".size").text(),
description: $(this).closest("td").siblings(".description").text()
}},
success: function (data){
console.log("Saved!");
}
});
}
PHP (save.php):
<?php
if (!empty($_REQUEST["action"]) && $_REQUEST["action"] == "save" && !empty($_REQUEST["elem"])){
$data = json_decode(file_get_contents("data.json"), true);
$data[0]["items"][] = $_REQUEST["elem"];
file_put_contents("data.json", json_encode($data));
}
?>
This is just a very small example. Additionally, you should take care about escaping, checking for the fields, error handling, simultanious updates (more than one client) etc. - but it shows a possible way to do this.