如何使用Javascript将表单数据存储到另一个页面的表中


How to Store form data to the table in another page using Javascript?

我需要使用javascript将表单数据从一个页面本地存储到表中的另一个页面?

javascript代码是

function addtext()
      {
         var fname = document.myform.first_name.value;
         var lname = document.myform.last_name.value;
         var email = document.myform.email.value;
         document.writeln("Thank you! You have just entered the following:");
         document.writeln("<pre>");
     document.writeln("<td>First Name :</td> " + fname);
         document.writeln("<td>Last Name :</td> " + lname);
         document.writeln("<td>Email Address :</td>  " + email);
    }

HTML部分是

     <table border="1" cellpadding="2" cellspacing="2">
        <tr>
        <form name="myform" action="confirm.php" method="get">
        <td>
          <label for="first_name">First Name </label>
          </td>
          <td>
          <input  type="text" name="first_name" maxlength="50" size="30">
            </td>
            </tr>
            <tr>
            <td>
          <label for="last_name">Last Name </label>
          </td><td><input  type="text" name="last_name" maxlength="50" size="30">
            </td></tr>
            <tr>
            <td>  <label for="email">Email Address</label>
          </td><td><input  type="text" name="email" maxlength="80" size="30">
            </td></tr><br>
          <input type="submit" value="Submit" onClick="addtext()">
        </form>
        </tr>
        </table>

检查JSFiddle!附件。希望它能帮助你。

var fname = document.myform.first_name.value;
     var lname = document.myform.last_name.value;
     var email = document.myform.email.value;
     document.writeln("Thank you! You have just entered the following:");
     document.writeln("<pre>");
 document.writeln("<td>First Name :</td> " + fname);
     document.writeln("<td>Last Name :</td> " + lname);
     document.writeln("<td>Email Address :</td>  " + email);

为什么要这样做?有什么特殊原因吗?虽然不是最佳实践,但如果它只是用户偏好而不是用户数据,那么通过javascript,您可以通过sessionStore或localStorage来实现。

请参阅Web存储

或者如果cookie很好,则使用jquery cookie

https://github.com/carhartl/jquery-cookie