简体   繁体   English

使用PHP将本地存储数据保存到服务器

[英]Save localstorage data to server using PHP

I'm trying to implement a "save later" functionality in jquery using localstorage. 我正在尝试使用localstorage在jquery中实现“以后保存”功能。 the idea is saving every form event in localstorage and using a button to sync them and save to the server. 我们的想法是在localstorage中保存每个表单事件,并使用按钮同步它们并保存到服务器。

I manage to save form data to localstorage and retrieve all the localstorage saved. 我设法将表单数据保存到localstorage并检索保存的所有localstorage。 Currently i'm struggling on how i can be able to submit specific values only to the server using PHP 目前我正在努力如何能够使用PHP仅向服务器提交特定值

Here's my code for saving form data to localstorage 这是我将表单数据保存到localstorage的代码

$('#systemInventory').on("submit", function(e) {
        e.preventDefault(); // prevent the form from attempting to send to the web server
        let $inputs = $('#systemInventory :input');

        let values = {};
        $inputs.each(function() {
            values[$(this).attr("name")] = $(this).val();
        });

        let title = 'systemInventory' + $.now();
        localStorage.setItem(title, JSON.stringify(values));
        $('#systemInventory')[0].reset();
        mdtoast('Successfully Saved',
            { type: mdtoast.INFO });
    });

and here's the one i used for retrieving data from the localstorage 这是我用来从localstorage中检索数据的那个

function allStorage() {

    let values = [],
        keys = Object.keys(localStorage),
        i = keys.length;

    while ( i-- ) {
        values.push( localStorage.getItem(keys[i]) );
    }

    return values;
}

Here's the one i'm trying to do to save specific data in the PHP storage 这是我想要在PHP存储中保存特定数据的那个

$.each(allStorage(), function (key, value) {
        let data = JSON.parse(value);
        console.log(data);
        $.ajax({
            url: "sync.php",
            method: 'POST',
            data: {data: data},
            success: function (response) {
                console.log(response);
            },
            error: function (response) {
                console.log(response);
            }
        });
    });

and my php code is 我的PHP代码是

if (isset($_POST['data'])){

    foreach ($_POST['data'] as $key => $value){

        $data[] = array(
            $key => $value
        );

    }

    $dataForm = call_user_func_array('array_merge', $data);

    print_r($dataForm);


//    print_r($data);

}

When console logging all the localstorage saved i have this result 当控制台记录所有已保存的localstorage时,我有这个结果 在此输入图像描述

and when clicking the sync, the one that is sending on the server i'm having this issue 当点击同步时,在服务器上发送的那个我有这个问题 在此输入图像描述

what i really want to happen is when i submit the localstorage data to php it will automatically form specific array for example 我真正想要发生的是当我将本地存储数据提交给php时,它将自动形成特定的数组

$data = array(
'type'=>'product',
'beginning'=>1,
'delivery'=>1

);

The problem is your allStorage() function is returning an indexed array, not an object, so you're getting only the values from local storage, not the keys too. 问题是你的allStorage()函数返回一个索引数组,而不是一个对象,所以你只得到本地存储的值,而不是键。

This means when you come to pass the data to PHP, it's ending up as an indexed, not associative, array, ie without keys. 这意味着当你将数据传递给PHP时,它最终会成为一个索引的,而不是关联的数组,即没有键。 This would be apparent if you ran print_r() on the received data, for example. 例如,如果您对接收的数据运行print_r() ,这将是显而易见的。

allStorage() can be fixed (and simplified) as: allStorage()可以修复(和简化)为:

function allStorage() {
    let data = {}; //<-- note object, not array
    for (var key in localStorage) data[key] = localStrage[key];
    return data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM