gpt4 book ai didi

javascript - 通过 AJAX 或 PHP 将 JavaScript 数据写入 JSON 文件

转载 作者:行者123 更新时间:2023-12-03 04:04:33 25 4
gpt4 key购买 nike

我有以下代码,它在表中显示有关人员的信息,当单击标记为“学习”的按钮时,该信息将存储在键/值类型数组中,然后将其拼接到指定的 JSON 对象中。

所有这些都工作正常,但我想更改保存 JSON 对象本身的文件,这意味着我需要处理服务器端代码。我如何将此插入到我的 JSON 对象中并实际更改它存储的文本文件(这样,如果我打开它,新信息就会在那里)?

我的代码如下-->

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..."
}
]
}
]

同样,在控制台中可以看到jsonData中添加了“jenkins”的信息;但是,文本文件本身保持不变,我希望它反射(reflect)这一添加。谢谢。

最佳答案

不可能使用 JavaScript 更改本地文件系统上的文件(至少在所有浏览器中)。我建议类似:

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(保存.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));
}
?>

这只是一个非常小的例子。此外,您应该注意转义、检查字段、错误处理、同时更新(多个客户端)等 - 但它显示了一种可能的方法来做到这一点。

关于javascript - 通过 AJAX 或 PHP 将 JavaScript 数据写入 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44618120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com