gpt4 book ai didi

PHP 变量到 PayPal JavaScript

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:45 24 4
gpt4 key购买 nike

我正在尝试将 PayPal 集成到我的网站上,但我似乎无法弄清楚世界上如何从 PayPal 中将总和输入弹出窗口。我试过插入 <?php echo $sum;?>直接进入 JavaScript,但这没有用。非常感谢任何帮助,并在此先感谢您!

我的代码:

<h2>Total:
<?php
include('includes/placeorder.php');
$sql = "SELECT SUM(product_price) as sum FROM cart WHERE sess_id='$id'";
$result = mysqli_query($con, $sql);
if ($result == false) {
$mysql_error = mysqli_error($con);
echo "There was a query error: $mysql_error";
} else {
while($row = mysqli_fetch_assoc($result)) {
echo $row['sum'];
}
}
?>
</h2>

来自 PayPal 的行:

<script>
paypal.Buttons({
style: {
shape: 'pill',
color: 'blue',
layout: 'vertical',
label: 'checkout',

},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '1'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>

我需要 amount:{value:''}是我在 MySQL 语句中提取行总和的总值。

最佳答案

您不是将结果分配给 $sum 变量,而是将其回显。

<h2>Total:
<?php
include('includes/placeorder.php');
$sql = "SELECT SUM(product_price) AS sum FROM cart WHERE sess_id = '$id'";
$result = mysqli_query($con, $sql);
if ($result == false) {
$mysql_error = mysqli_error($con);
echo "There was a query error: $mysql_error";
} else {
$row = mysqli_fetch_assoc($result);
$sum = $row['sum']; // Assign the result to $sum to use it in your code
echo $sum; // If you want to show total amount
}
?>
</h2>
<script>
paypal.Buttons({
style: {
shape: 'pill',
color: 'blue',
layout: 'vertical',
label: 'checkout',

},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '<?= $sum; ?>'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>

关于PHP 变量到 PayPal JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58316305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com