0

I'm trying to create a "Same As Shipping" Button that's seen on many online ordering platforms that, when clicked/checked during the billing step of the transaction, imports the information from another page. I looked at the other question on this topic and didn't find anything helpful. I do know that the two pages fall under the Same Origin Policy.

I've got the Javascript code created to populate the billing page with information. However, I cannot find a way to bring the value from the other page.

<script language="JavaScript">
function SameAs(){
    document.getElementById("FIELD_4").value = "752";
    document.getElementById("FIELD_6").value = "50";
}
</script>

My problem is that I can't bring down the element from the previous page to use as the value.

Community
  • 1
  • 1
9Deuce
  • 689
  • 13
  • 27
  • 4
    Stop looking. It's not possible to get the value of an element of a 'previous' page. – wvdz Dec 17 '14 at 19:55
  • try local storage or query string or global variable – aaviss Dec 17 '14 at 19:56
  • They aren't "bringing it from another page." They are just either flagging that the billing is the same as shipping, or they are going to the database and getting the shipping info to populate the billing. – Jack Dec 17 '14 at 19:56
  • If it is a previous page (not in the same view), I'm assuming the question is how to persist data between pages? You would need to store it somewhere you can retrieve it from. A fiddle or live example would be helpful in answering your question. – Ted Dec 17 '14 at 19:56
  • @Jack Could you submit that as an answer, because that's actually a good idea that's doable. Thanks for your help. – 9Deuce Dec 17 '14 at 20:06
  • this problem is best solved on the server side, if you are talking about sensitive information that the user posted on the previous page. This is not say it can't be done on the client side – code_monk Dec 17 '14 at 20:07

3 Answers3

1

Save it in local storage on the first page

localStorage.setItem('city', $('#elementID').val());

Then retrieve it from the second

localStorage.getItem('city');
Steffan Perry
  • 2,112
  • 1
  • 21
  • 21
0

You're trying to fetch an element from a page that you are no longer on? This is impossible. You can only get elements that currently exist in the DOM.

Brennan
  • 5,632
  • 2
  • 17
  • 24
0

You need to store the information either in an encrypted cookie (for privacy reasons, you should never save payment information though) or request it via AJAX from the second page. There is no way to get the information from a previously submitted page.

Another option is not to actually submit a form from the prior page, but instead show the next page by making it visible via Javascript. When both "pages" are ready, then you can submit the form.

Keith
  • 984
  • 6
  • 9