PHP在同一页面上传递两个脚本的数据

时间:2017-10-01 17:30:51

标签: php

如何将HTML上方的PHP代码中的数据传递给HTML下面的PHP代码?我需要HTML下面的PHP代码,因为运行的代码不会在HTML之前运行。

热门PHP:

<?php
    $_SESSION['amount'] = null;


    if(isset($_POST['moneyBTN'])){

    $pack = $_POST['package'];
      if($pack == "1"){
        $_SESSION['amount'] = 600;

        $data_amount = 600;
        $data_name = "Example1";
      }
       if($pack == "2"){
        $_SESSION['amount'] = 1000;

        $data_amount = 1000;
        $data_name = "Example2";
      }
       if($pack == "3"){
        $_SESSION['amount'] = 1300;

        $data_amount = 1300;
        $data_name ="Example3"; 
      }

    }
    ?>

HTML下面的PHP:

<div class="card-block">
        <h4 class="card-title">Title</h4>
        <p class="card-text">Desc</p>

        <iframe width="0" height="0" border="0px solid white" 
style="display:none" name="target1" id="target1"></iframe>

          <form action="shop.php" method="post" target="target1" >

        <div class="form-group input" >

                <select name="package" class="form-control" id="select">

                  <option>1</option>
                  <option>2</option>
                  <option>3</option>


                </select>
              </div>
        <input type="submit" class="btn btn-primary" name="moneyBTN" 
    value="Continue">
        <div>
         </form>
        <div  ">
          <form action="shop.php" method="post">
          <script
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="pk_test_xxxxxxxxxxxxxxxxxx"
          data-amount=<?php echo $data_amount;?>
          data-name=<?php echo $data_name;?>
          data-description="Buy"
          data-
image="https://stripe.com/img/documentation/checkout/marketplace.png"
          data-locale="auto">
          </script>
        </form>
        </div>
        </div>

      </div>
    </div>

以下是php代码:

<?php
      $amount = $_SESSION['amount'];
    require_once('vendor/autoload.php');
    \Stripe\Stripe::setApiKey("sk_testxxxxxxxxxxxxxxxxx");
    $token = $_POST['stripeToken'];

      $charge = \Stripe\Charge::create(array(
      "amount" => $amount,
      "currency" => "usd",
      "description" => "Buy",
      "source" => $token,
      ));

    ?>

目前,这不起作用。我怎样才能使它工作?根据所选的数量,金额具有不同的值。此值确定输入条带代码的金额。

2 个答案:

答案 0 :(得分:2)

如果两个代码都在同一页面上(即在同一个PHP文件中),则变量应该可以在许多PHP代码块上使用,因为大多数PHP变量具有相同的范围。

参考:http://php.net/manual/en/language.variables.scope.php

如果我们看到完整版的代码,这将有助于找到问题的原因。

答案 1 :(得分:0)

根据您的输入,我制作了这个脚本。

<?php
session_start();

$_SESSION['amount'] = null;
$data_amount = null;
$data_name = null;

if(isset($_POST['moneyBTN'])){
    $pack = $_POST['package'];
    if($pack == 1){
       $_SESSION['amount'] = 600;
       $data_amount = 600;
       $data_name = "Example1";
    }
    if($pack == 2){
       $_SESSION['amount'] = 1000;
       $data_amount = 1000;
       $data_name = "Example2";
    }
    if($pack == 3){
       $_SESSION['amount'] = 1300;
       $data_amount = 1300;
       $data_name ="Example3"; 
    }
}

?>
<html>
    <head>
        <title>Testing</title>
    </head>
    <body>

        <div class="card-block">
            <h4 class="card-title">Title</h4>
            <p class="card-text">Desc</p>            
            <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                <div class="form-group input" >
                    <select name="package" class="form-control" id="package">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                </div>
                <input type="submit" class="btn btn-primary" id="moneyBTN" name="moneyBTN" value="Continue">
            </form>
            <iframe width="150px" height="50px" style="border:solid 1px #fff;" name="target1" id="target1">
                <?php echo $_SESSION['amount']." ".$data_amount." ".$data_name;?>
            </iframe>
        </div>
        <div>
            <?php echo $_SESSION['amount']." ".$data_amount." ".$data_name; ?>
            <form action="shop.php" method="post">
                <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
                    data-key="pk_test_xxxxxxxxxxxxxxxxxx"
                    data-amount=<?php echo $data_amount;?>
                    data-name=<?php echo $data_name;?>
                    data-description="Buy"
                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                    data-locale="auto">
                </script>
            </form>
        </div>
    </body>
</html>





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