如何将sessionStorage作为变量存储,以便可以将其存储在数据库中

时间:2018-11-10 16:56:14

标签: javascript php

我需要存储已存储的登录用户的电子邮件 带有代码行的sessionStorage:

sessionStorage.setItem("loggedInUser", $("#email").val());

但是在另一个程序中,我需要将电子邮件存储为某种东西,以便可以对其进行排序并发布到将其插入到我的数据库中的php文件中。到目前为止,这是我的JavaScript。

$(document).ready(function () {
        //When the submit button on the recruitment form is pressed.
        $("#SubmitRecruit").on("click", function () {
                //store each individual entry into a separate variable.
                var email = sessionStorage.loggedInUser;
                var forename = document.getElementById("forename").value;
                var surname = document.getElementById("surname").value;
                var jobType = document.getElementById("jobType").value;
                var phoneNo = document.getElementById("phoneNo").value;

                //create an array with the details in.
                var recruitdetail = {
                    email: email,
                    forename: forename,
                    surname: surname,
                    password: password,
                    phoneNo: phoneNo,
                }
                //log the forename in the console so that we know that the register has been successful.
                console.log(email)
                //direct the user to the login page and alert them that their registration was successful.
                window.location.href = "../index.html"
                alert("Your recruitment post was made, thank you. We will get back to you shortly.")
                //posts the JSON object to the php file so it can fill the database, and converts the register array into JSON so it can be read. 
                var jqxhr = $.post("../php/recruit.php", JSON.stringify(recruitdetail)

                )
            }
        })

在第五行,我试图使其等于sessionStorage.loggedInUser,但无济于事。我知道我肯定需要将电子邮件存储为会话中的某种内容,因为那样的话,它将被传递回php并存储在db中,但是我不知道该怎么做或如何做。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

要从会话存储中获取项目,请尝试使用getItem方法:

var email = sessionStorage.getItem("loggedInUser");

请参见sessionStorage documentation

对于ajax请求,只需将对象直接传递到$.post并将警报放入成功回调中,以便仅在请求成功完成时显示警报:

//posts the JSON object to the php file so it can fill the database, and converts the register array into JSON so it can be read. 
var jqxhr = $.post("../php/recruit.php", recruitdetail, function(data) {
    alert("Your recruitment post was made, thank you. We will get back to you shortly.")
});

作为旁注-如果您有一个用户/登录名表,则每个用户的电子邮件应存储在该表中。然后,您的用户表和其他表之间的外键将是一个(自动递增)ID,而不是电子邮件地址。这使系统更加灵活(可以更改电子邮件等)。

答案 1 :(得分:0)

为了在数组中使用本地存储,我们需要使用一种使我们稍后轻松进行转换的方法将数组转换为字符串。将数组转换为字符串的方法是使用JSON stringify函数。

假设您有以下称为电影的数组:

各种电影= [“储水狗”,“低俗小说”,“杰基·布朗”,               “杀死比尔”,“死亡证明”,“无耻混蛋”]; 使用stringify函数,可以使用以下语法将电影数组转换为字符串:

JSON.stringify(电影) 由于我们希望将所有内容都存储到本地存储中,因此将它们放在一起可以得到以下信息:

各种电影= [“储水狗”,“低俗小说”,“杰基·布朗”,               “杀死比尔”,“死亡证明”,“无耻混蛋”];

localStorage.setItem(“ quentinTarantino”,JSON.stringify(movies));

答案 2 :(得分:0)

如果通过不同的程序表示不同域上的不同应用程序,则可以尝试通过AJAX请求发送电子邮件值以存储在PHP会话中。

如果在同一域中,但只是在不同选项卡中运行的其他应用程序,则可以使用localStorage,其语法与sessionStorage相同。请注意,sessionStorage数据仅限于设置了该标签的标签。

localStorage.setItem("loggedInUser", $("#email").val());

然后

        var email = localStorage.loggedInUser;

或者您可以使用cookie。 https://www.w3schools.com/js/js_cookies.asp