繁体   English   中英

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

[英]How to store sessionStorage as a variable so it can be stored in a database

我需要存储已登录用户的电子邮件,并使用以下代码行将其存储到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中,但是我不知道该怎么做或如何做。 任何帮助将不胜感激。

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

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

请参阅sessionStorage文档

对于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,而不是电子邮件地址。 这使系统更加灵活(可以更改电子邮件等)。

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

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

var movie = [[“ Reservoir Dogs”,“ Pulp Fiction”,“ Jackie Brown”,“ Kill Bill”,“ Death Proof”,“ Inglourious Basterds”]; 使用字符串化功能,可以使用以下语法将电影数组转换为字符串:

JSON.stringify(movies)由于我们要将所有内容存储到本地存储中,因此将它们放在一起时,将得到以下内容:

var movie = [[“ Reservoir Dogs”,“ Pulp Fiction”,“ Jackie Brown”,“ Kill Bill”,“ Death Proof”,“ Inglourious Basterds”];

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

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

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

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

接着

        var email = localStorage.loggedInUser;

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

暂无
暂无

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

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