gpt4 book ai didi

javascript - 如何用ajax将本地存储的数据发送到php页面?

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

我已将表单数据存储在本地存储数组中,我想使用ajax将该数组发送到php页面,并且还想访问php页面中的数据。
这是我的代码 -

    var myvalue=document.getElementById('name').value;        
var favorites_str = localStorage.getItem('my_favorites');
if(favorites_str == null) {
favorites = [];
favorites.push({ "name":myvalue});
}
else{
favorites = JSON.parse(favorites_str);
favorites.push({ "name":myvalue});
}
localStorage.setItem('my_favorites',JSON.stringify(favorites));
var data = localStorage.getItem('my_favorites');
if(data == null){
alert("0 favorites");
}else{
favorites = JSON.parse(data);
$.each(favorites, function(index,item){
var my_items=item.name;
})
};
if(navigator.onLine)
{
$.ajax({
url:'http://localhost/offline/nextpage.php',
type:'post',
data:{my_items:my_items},
success:function(data)
{
$('#result').html(data);
}
});
}

谢谢。

最佳答案

假设您将数据存储在本地存储中,键名称为:“name”,您可以像下面这样获取它,并将其发送到ajax调用中。

data: {name: localStorage.getItem('name')}

关于javascript - 如何用ajax将本地存储的数据发送到php页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40217579/

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