简体   繁体   English

将输入表单数据发送到不同页面上的登录表单(HTML/js)

[英]Send input form data to login form on different page (HTML/js)

I have a 2-part login where you enter your username on the first page, then, the second page opens via window.open() and I want to autofill the username data from the first page into the username field on the second page.我有一个两部分登录,您在第一页输入您的用户名,然后,第二页通过window.open()打开,我想将第一页的用户名数据自动填充到第二页的用户名字段中。

Here's my code (1st page):这是我的代码(第一页):

    const $document = $(document);
        const $login = $("#login");
        const $loginBox = $("#loginbox");

        const $formElement = $(`
          <div class="loginContainer">
            <label for="userNameBox"><b>Username: </b></label>
            <input type="text" class="userName"id="userNameBox" placeholder="Enter Username"required="required">
            <input type="button" class="loginButton" id="loginButton" value="Login"/>
          </div>
        `);

        $document.on("click", "#loginButton", function() {
          const $userNameBox = $("#userNameBox");

          if ($userNameBox.val() !== "") {
            window.open('secondPage.com','_blank');
          } else {
            alert("Please Enter a Username.");
            location.reload();
          }
        });

          $('.login').click(function(e) {

          if ($loginBox.children().length === 0) {
            $loginBox.append($formElement);

          }

          $("#loginbox").show();

          event.preventDefault();
          return false;
        });

The second page is just a standard login page with username and password fields and are set up like <input type="text" id="USERNAME2" name="USERNAME2" placeholder="username" class="text_field item" >第二个页面只是一个标准的登录页面,包含用户名和密码字段,设置类似于<input type="text" id="USERNAME2" name="USERNAME2" placeholder="username" class="text_field item" >

Can I use a session cookie to somehow send the form data over to the second page?我可以使用 session cookie 以某种方式将表单数据发送到第二页吗?

As a backend developer we stores the temporary information in the session and you can get the help of storing sessions in js in this thread here And when the next page is loaded then you can load the session data using the same process.作为后端开发人员,我们将临时信息存储在 session 中,您可以在线程中获得在 js 中存储会话的帮助当加载下一页时,您可以使用相同的过程加载 session 数据。

// Save data to sessionStorage
sessionStorage.setItem('dataStored', data.xhr.response);

// Get saved data from sessionStorage
var data = sessionStorage.getItem('dataStored');

// Remove saved data from sessionStorage
sessionStorage.removeItem('dataStored');

// Remove all saved data from sessionStorage
sessionStorage.clear();

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

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