Jump to content
n1concepts

How to capture JS value in var then pass to PHP fiile

Recommended Posts

Hi,

I'm working with an example set of code to create a Stripe (Token) and that script works Great!

Example code found at https://jsfiddle.net/ywain/5yz4z2yn/

However, I need to capture the 'token' which is found at line 38 within the Javascript code.

My issue: how can I pass that var (result.token.id) to an external PHP file?

If you want to see the simulation - just fill out the form (use 4242 4242 4242 4242 for the test card & current of future 'two-digit' year) other values are random.

In the HTML code (simulated form) you'll see that the 'token' class display the token when (result.token) JS condition is met / TRUE

<div class="success">
        Success! Your Stripe token is <span class="token"></span>
      </div>

I'll suppress the token from showing the var once I am able to grab the value & pass into the, external, PHP (Stripe api's for processing).

Again, here's the code snippet in question from the 'Javascript file found at https://jsfiddle.net/ywain/5yz4z2yn/

if (result.token) {
    // Use the token to create a charge or a customer
    // https://stripe.com/docs/charges
    successElement.querySelector('.token').textContent = result.token.id;
    successElement.classList.add('visible');

Again, I need to (somehow) grab 'result.token.id' and (securely) pass that value to an external PHP file.
Note: I do not want to use cookies b/c that not a stable solution for all browsers nor safe.

Appreciate any suggestions - thx!

Edited by n1concepts

Share this post


Link to post
Share on other sites

Put it into a hidden input with the rest of the form that I assume you're using.

1 hour ago, n1concepts said:

Note: I do not want to use cookies b/c that not a stable solution for all browsers nor safe.

And you think that Javascript is? Spoiler: it isn't.

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, requinix said:

Put it into a hidden input with the rest of the form that I assume you're using.

Or use AJAX. Depends on what you're trying to accomplish and how you're trying to accomplish it.

As far as the security goes, no matter what you do that part's going to be up to you, and I'd recommend not just blindly copy/pasting code from the internet in this situation.

  • Like 1

Share this post


Link to post
Share on other sites

Thx for response requinix && maxxd - both your comments (I agree & on point).

I would prefer to handle this on the backend with php, however, not my call - lead devs prefer JS so reason I was inquiring (thx)

For the code (copying from WWW) - yeah, use to boiler plate customization (certainly not using 'as is' - thx again for respnose).

We got it working but not how I would have preferred - but always multiple ways to accomplish similar results.

Edited by n1concepts

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.