将localStorage数据发送到PHP [英] Sending localStorage data to PHP

查看:114
本文介绍了将localStorage数据发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些研究,甚至有一本书解释了Ajax对php的调用以及与浏览器和服务器之间的数据发送.我发现这与我的需求非常接近,因为我正在使用本地存储如何将数据从Javascript传递到PHP,反之亦然?

I've done some research and even have a book that explains Ajax calls to php and sending data to and from browser and server. I found this to be pretty close to what I need since I am using local storage How can I pass saved localStorage web data to a php script?, this link seems pretty coherent but I am not sure what to do on the PHP side How to pass data from Javascript to PHP and vice versa?,

尽管我有一个更大的问题,但我在本地存储中的数据量非常大,有几个不同的数组存储在其中,并进行了JSON编码,当我得到该数组时,我使用JSON.parse进行设置我将数组存储在数组中.如何发送此类数据呢?并不是localStorage中的所有数据也都是数组,本地存储中有数百个变量需要保存.

though I have a bigger problem, the amount of data I have in local storage is pretty large, there are several different arrays that are stored there and are JSON encoded, when I get the array I use JSON.parse and to set the array in storage I stringify it. How could this be done to send that kind of data over? Not all the data in localStorage is an array either, there are hundreds of variables in local storage that need to be saved.

这是我的代码,因为它引起了我的注意,因为它是必需的.我正在尝试从localstorage获取所有数据,然后将其发送到服务器以保存在数据库中.

Here is my code since it was brought to my attention that it is needed. I am trying to get all the data from localstorage to be sent to the server to be saved in a database.

function postData(){

    var storage = JSON.stringify(localStorage);

    var xhr;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
            }
        }
    };

    xhr.open('POST', 'Membership.php');
    xhr.setRequestHeader("Content-Type: text/json", "application/x-www-form-urlencoded");
    xhr.send("data=" + storage);

}

在我拥有的PHP方面

if ($_POST){

    $data = json_decode($_POST['data']);

    echo "The data" .$data;

}

我不知道这是否正确,我无法找到有关如何传递所有本地存储数据的任何信息,这基本上是我之前试图询问的内容,但显然未能传达出来.

I don't know if this is correct, I can't find anything on how to pass all localstorage data, this is basically what I was trying to ask before but obviously failed to convey that.

推荐答案

基本上,您需要做的是:

Basically what you need to do is:

  1. 从localStorage检索所有数据
  2. 对所有内容进行字符串化(现在,您具有所有数据的JSON表示形式)
  3. 使用jQuery或other_framework_of_choice准备AJAX请求,并将有效负载设置为字符串化数据并发送:

  1. Retrieve all the data from localStorage
  2. Stringify everything (Now you have JSON representation of all the data)
  3. Prepare the AJAX request using jQuery or other_framework_of_choice and set the payload to the stringified data and send it:

  • 发送内容类型:text/json ti向服务发送提示,告知您正在发送JSON数据(可选,但推荐)
  • Send Content-type: text/json ti send a hint to the service that you are sending JSON data (Optional, but recommended)

在PHP端处理数据(即有效载荷 json_decode 并使用您自己的逻辑来满足需要来处理它.

Process the data within the PHP side (I.e json_decodethe payload and process it using your own logic satisfying your needs.

注意:

您没有提供任何代码,所以我只跳过编码部分(我猜不到您的代码,队长)

NOTE:

You didn't provided any code, so I would just skip the coding part (I can't guess your codes, mate)

您可以参考

You can refer to Send data from localStorage via AJAX to PHP and save it in an HTML file just skip the saving part and replace with your desired processing

这篇关于将localStorage数据发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆