javascript copy form from one page to another form

2019-06-11 02:16发布

问题:

Is it possible to copy values from a form on page x to a form on page y using javascript. The data is the same but just different pages and different applications?

回答1:

Yes, you can do this. Using AngularJS you could use "search" variables and then plug these into the form. Using normal javascript you may be able to use a # with the data. Using PHP + normal javascript (more secure) you can send a POST to the page (which is a PHP page) and have the page, if the POST params with your values exist, put a javascript fragment on the page setting the values of the form when the page is done loading.

Cookies are also always an option :)

[EDIT]

After discussion, here is the solution to the question: GitHub



回答2:

Actually, cross-tab JS is not allowed. I am not sure about bleeding-edge apis.

Theoretically, it is possible to do via local storage with a same-origin restriction. But you'd better don't mind this option.

Also, try to play with cookies.



回答3:

Try something like this:

window.onload = function() {
   var myValues;
   if (document.cookie) {
      // get your values onLoad
      myValues = document.cookie;
   } else {
      // set your values
      myValues = "..." // your values
      document.cookie = myValues;
   }
}


回答4:

If it's just from one page to another you can append the data to the request as POST (with PHP) variables, or a simple query string (GET) and read them once the page has loaded/populate relevant fields - if the second page is loaded directly from the first.

If it needs to be retained until an unspecified time (when the user may have gone to other pages in the interim) you could look into using cookies.

how to get GET and POST variables with JQuery?