gpt4 book ai didi

javascript - 将本地存储值插入数据库

转载 作者:行者123 更新时间:2023-11-30 22:24:07 25 4
gpt4 key购买 nike

希望你今天过得愉快我怎样才能将本地存储值插入数据库,这是我正在使用的代码

function fun() {
var count = localStorage.getItem("count") || 0;
localStorage.setItem("count", ++count);
}

function result() {
document.write("your mark is " + (localStorage.getItem("count") || 0) + " from 9")
}

function clear(){
window.localStorage.clear();
}

我想将变量 count 的值插入到数据库中..这可能吗?

最佳答案

这是您的答案 - 它更详细地解释了如何使用帖子将数据从 javascript 获取到您的数据库或获取您创建的端点,它将 localStorage 的值写入您的数据库。

关键是您需要使用客户端调用来进行服务器写入,这通常使用以下答案中描述的方法来完成。如果没有,它应该可以帮助您更有效地搜索,找到与此相关的不同方法,可能适合您。

How to pass data from Javascript to PHP and vice versa?

这段代码可能有助于解释这个想法:

function result() {
var resultText = "your mark is " + (localStorage.getItem("count") || 0) + " from 9";
document.write(resultText);
// set the full text into the hidden field value, or just do .val(localStorage.getItem("count")) if you only want the count.
$("#hdnFieldName").val(resultText);
}

<input type="hidden" id="hdnFieldName" name="hdnFieldName" value="" />
<input type="submit" text="Submit button" onclick="result();" />

关于javascript - 将本地存储值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811326/

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