如何根据HTML表单上的用户输入更新我的PHP脚本

时间:2017-07-24 08:16:25

标签: javascript php html forms stripe.net

This is how it is submitted, once you click pay with card a popup modal appears to enter payment details我正在尝试使用条带API 创建自定义付款。

我希望用户能够在表单中输入任何号码进行付款,并让我的php获取输入,以便为所选金额的卡收费。

在我的PHP代码中,"金额通常设置为固定数字,如50.00" ,但我希望它依赖于html表单中的用户输入。

这是我到目前为止所拥有的。

由于

<form action="testprocess.php" method="POST">
<h3> Enter Payment Amount</h3> 

<input type="number" name="pay" id="pay">

<script
   src="https://checkout.stripe.com/checkout.js" class="stripe-button"
   data-key="pk_test_00000000000"
   data-amount=""
   data-name="test"
   data-description="text">
</script>
</form>


     ___________________


 <?php

  // See your keys here: asgfcvbjh


  \Stripe\Stripe::setApiKey("sk_test_0000000000000");

   // Token is created using Stripe.js or Checkout!
   // Get the payment token submitted by the form:

   // Charge the user's card:

  $pay = $_POST['pay'];


  $charge = \Stripe\Charge::create(array(
    "amount" => $pay,
    "currency" => "usd",
    "description" => "test",
  ));


     ?>

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式执行此操作,您必须创建自定义结帐。积分转到this guy

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 <script src="https://checkout.stripe.com/checkout.js"></script>

Amount: <input type="text" id="stripeAmount">
<button class="btn btn-success" id="stripe-button">
    Checkout
</button>

<script>
    $('#stripe-button').click(function () {
        var token = function (res) {
            var $id = $('<input type=hidden name=stripeToken />').val(res.id);
            var $email = $('<input type=hidden name=stripeEmail />').val(res.email);
            $('form').append($id).append($email).submit();
        };

        var amount = (Number($("#stripeAmount").val()) * 100);
        StripeCheckout.open({
            key: '<?= STRIPE_PUBLIC_KEY ?>',
            amount: amount,
            name: 'Serendipity Artisan Blends',
            //image: 'path to yoru image',
            description: 'Purchase Products',
            panelLabel: 'Checkout',
            token: token
        });

        return false;
    });
</script>

<强>输出 enter image description here