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条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法