doujie4050 2017-10-02 02:56
浏览 67
已采纳

变量正在更新但不被识别PHP

Even though the variable is properly being changed by the if statements when a button is pressed, the original value is inputted. In the code, when a button is pressed, a value called pack is changed based on the input. The amount is based on this value. The button and $_POST are working properly, as well as the if statements because the respective values are being echoed. However, the $amount in the Stripe integration code uses the value used when the variable was created.

//Variable is set
$pack = 0;

if( isset( $_POST['moneyBTN'] )) {
    $pack = $_POST['package'];
}

 $amount = (int)$_POST['packages'];
//Stripe integration
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxx");
$token = $_POST['stripeToken'];
$charge = \Stripe\Charge::create(array(
    "amount" => $amount,
    "currency" => "usd",
    "description" => "Buy",
    "source" => $token,
));

HTML

<form action="shop.php" method="post" >
    <div class="form-group input">
        <select name="package" class="form-control" id="select">       
          <option value="600">1</option>
          <option value="1000">2</option>
          <option value="1300">3</option>    
         </select>
     </div>
     <input type="submit" class="btn btn-primary" name="moneyBTN" value="Continue">
</form>

<form action="shop.php" method="post">
    <script 
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="pk_test_FFqjZfFYCf32C51gmAhR2Ey6"
          data-amount=600
          data-name=Example
          data-description="Buy!"
          data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
          data-locale="auto">
    </script>
</form>

Thank you for your help.

  • 写回答

3条回答 默认 最新

  • dongshan4878 2017-10-02 03:29
    关注
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
        $amount = (int)$_POST['package']; //you had an 's' on 'packages' here.
    
        //Stripe integration
        require_once('vendor/autoload.php');
        \Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxx");
        $token = $_POST['stripeToken'];
        $charge = \Stripe\Charge::create(array(
            "amount" => $amount,
            "currency" => "usd",
            "description" => "Buy",
            "source" => $token,
        ));
    
        if($charge['success_message']) { //example, not working or tested
            echo 'success';
        } else {
            echo 'failure';
        }
    
    } else { ?>
    
    <form action="shop.php" method="post" >
        <div class="form-group input">
            <select name="package" class="form-control" id="select">       
                <option value="600">1</option> 
                <option value="1000">2</option> 
                <option value="1300">3</option>   
            </select>
        </div>
        <script 
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="pk_test_FFqjZfFYCf32C51gmAhR2Ey6"
          data-amount=600
          data-name=Example
          data-description="Buy!"
          data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
          data-locale="auto">
        </script>
    </form>
    
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改