doulu4316 2019-04-16 16:22
浏览 104

如何在不提交表单的情况下将js变量赋值给php变量

I am trying to integrate Stripe's checkout. I am using https://checkout.stripe.com/checkout.js.
I have a amount's input box on the form and I want to pass that input box's value to Stripe's script. <input id="amount" name="amount" type="text" class="form-control" placeholder="Enter amount" onKeyUp="calculate()" pattern="^[0-9]*$">

Heres my stripes script

        <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="<?php echo $stripedetails['publishable_key']; ?>"
        data-amount="1000"//<-Amount here 
        data-name="KAEM Technologies USA, Inc"
        data-email="<?php echo $email;?>"
        data-currency="<?php echo $curr_code; ?>"
        data-description="Widget"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                            data-locale="auto">
                          </script>

I tried using COOKIES , I tried to assign js function's var to php using

  var x = document.getElementById('amount').value;
  <?php $amount = "<script>document.write(x)</script>";?>

So how can I pass input box's value to stripe's script without submitting the form. Thanks
@barmar heres the code I tried:

 <button type="submit" id="custombutton" class="btn btn-primary" ><i class="fa fa-cc-stripe"></i>&nbsp;Pay Via Card&nbsp;&nbsp;</button>
                         <script src="https://checkout.stripe.com/checkout.js"></script>
                          <script>
                            var handler = StripeCheckout.configure({
                              key: 'pk_test_DzL83GJnn9U4lMBuk311P1hK0026zJJrLW',
                              image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
                              locale: 'auto',
                              token: function(token) {
                                // You can access the token ID with `token.id`.
                                // Get the token ID to your server-side code for use.
                              }
                            });

                            document.getElementById('customButton').addEventListener('click', function(e) {
                              // Open Checkout with further options:
                              handler.open({
                                name: 'KAEM Technologies (USA), Inc.',
                                description: '2 widgets',
                                amount: 2000
                              });
                              e.preventDefault();
                            });

                            // Close Checkout on page navigation:
                            window.addEventListener('popstate', function() {
                              handler.close();
                            });
                            </script>

Is this right ?

  • 写回答

1条回答 默认 最新

  • doufenzhu7012 2019-04-16 16:36
    关注

    Use the "custom" integration described here, where you call Stripe checkout methods from JavaScript instead of putting everything into the <script> tag.

    You can then fill in the amount: parameter from the form.

    document.getElementById('customButton').addEventListener('click', function(e) {
      // Open Checkout with further options:
      handler.open({
        name: 'KAEM Technologies USA, Inc',
        description: 'Widget',
        amount: document.getElementById('amount').value,
        email: "<?php echo $email;?>"
      });
      e.preventDefault();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题