apaul apaul - 11 months ago 112
PHP Question

PHP pass data beteen two scripts on the same page

How can I pass data from PHP code above the HTML to the PHP code below the HTML? I need the PHP code below the HTML since the code being run will not run before the HTML.

Top 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";
}

}
?>


The PHP below the HTML:

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


The following is the php code:

<?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,
));

?>


Currently, this is not working. How can I make it work? Based on the number selected, the amount has a different value. This value determines how much money is inputted into the Stripe code.

Answer Source

Based on your inputs, I made this script.

<?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,
));
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download