0

I have to use a simple form to retrieve a token from a server. I then need to use that token to create a unique link for a user session. I have the following sample code to generate the token page:

<form id="token" action="whatever.php?name=joe&hobbie=fun" method="POST"></form>
<script>
    document.getElementById('token').submit();
</script>

This automatically takes me to the page that has the token on it as a text string. I just need to figure out how to get that text as a variable I can use.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Are you asking how to implement server-side functionality in `whatever.php`? How to fetch the token in code without refreshing the page? Something else? It's not really clear what you're asking. – David Jun 15 '16 at 19:36

2 Answers2

0

In your handler for whatever.php, you're redirecting the user to a new page and then rendering some HTML. You can pass the token along as a query parameter on the new page, and then grab that from javascript if you need to store it and use it locally.

This answer shows you how to retrieve a value off the query string.

Community
  • 1
  • 1
mariocatch
  • 8,305
  • 8
  • 50
  • 71
0

If you cannot for whatever reason add the token to the page as a hidden value in the following example:

<input type="hidden" value="<?php echo $token; ?>" name="token" id="token" />

Then please use AJAX for this with the following logic (All based off your code):

  • Have a server side script which produces the token lets call this page token.php
  • Send to token.php a post request using AJAX with your variables for generating the token (Seems like you send name and hobby)
  • Receive the token from the scripts response and use it as you would like

An easy and simple AJAX tutorial can be found here http://www.w3schools.com/ajax/

Bubble Hacker
  • 6,425
  • 1
  • 17
  • 24