簡體   English   中英

通過PHP添加到變量將無法正常工作

[英]Adding to variable through php won't work

我有一個php腳本,用於為支付系統的信用卡收費,但問題是我有一個帶有變量的腳本,每次調用php腳本中的函數時,我都試圖將其加1。是它不起作用。 我不確定代碼是否有問題,或者是因為加載新頁面時未保存變量,因為每次調用php腳本時,它都會重定向到新頁面。

帶有名為product1.html的變量的html腳本

<script type="text/javascript">

                var currentBid = 1;
                document.getElementById("currentBid").innerHTML = currentBid;

                function addOne() {
                    currentBid = currentBid +1;
                    document.getElementById("currentBid").innerHTML = currentBid;
                }

            </script>

帶有名為chargeCard.php的函數的php腳本

    <?php 

    require_once('./stripe-php/init.php');

    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here https://dashboard.stripe.com/account
    \Stripe\Stripe::setApiKey("Removed for safety");

    // Get the credit card details submitted by the form
    $token = $_POST['stripeToken'];
    $myAmount = $_POST['amount'];
    $describtion = $_POST['description'];

    $myAmount = round((int)$myAmount*100,0);

    // Create the charge on Stripe's servers - this will charge the user's card
    try {
    $charge = \Stripe\Charge::create(array(
    "amount" => $myAmount, // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => $describtion));

    echo '<script type="text/javascript">addOne();</script>"';

    } catch(\Stripe\Error\Card $e) {
    // The card has been declined
    }

?>

我試圖在php腳本中調用的函數是回顯行echo '<script type="text/javascript">addOne();</script>"';

我第一眼看到的是一個語法錯誤:

您的雙引號過多(“)。

可以嗎?

正確的回聲是:

echo '<script type="text/javascript"> addOne(); </script>';

看來您正在嘗試將JavaScript變量從一頁轉移到另一頁。 不幸的是,一旦離開當前頁面的范圍,JavaScript變量就會消失。

看起來您已經在使用表單將POST發送到服務器,因此,可以維護數據狀態的方法是在表單中的服務器和客戶端之間來回傳遞。

例如:

在HTML中,假設您當前的表單結構如下:

<form method="POST" action="/enterYourHandlerUrlHere">
    <input type="hidden" name="stripeToken">
    <input type="text" name="amount">
    <input type="text" name="description">
    <!-- This is the important line -->
    <input type="hidden" name="currentBid" value="<?php echo $currentBid ?>">
</form>

在您的PHP中,在將以上內容呈現給瀏覽器之前...

<?php

    $currentBid = (isset($_POST['currentBid']) ? $_POST['currentBid'] + 1 : 0);

當您將HTML呈現到屏幕上時,$ currentBid值將被臨時存儲在頁面上的隱藏輸入中,可以在客戶端上本地訪問該值,並且您的后端邏輯以及您只要保持連鎖。

另外,可能最好將變量存儲在$ _SESSION超全局數組中,如下所示:

if (!isset($_SESSION['currentBid'])) {
    $_SESSION['currentBid'] = 0;
} else {
    $_SESSION['currentBid']++;
}

這將在多個Web請求中維護您的數據,因為PHP具有這個漂亮的超全局變量來跟蹤用戶數據。 有關配置和使用$ _SESSION變量的其他信息,請訪問以下鏈接以獲取官方文檔:

PHP:會話

Session解決方案可能需要更多工作,但是絕對值得!

關鍵概念:
1. php在服務器端運行。 2. javascript在客戶端上運行

問題:服務器沒有由javascript設置的變量,因為它是在客戶端設置的。

方法:您的javascript需要將變量發送到服務器,然后您的php才能使用來自javascript的變量來處理腳本。

方法:使用隱藏形式,並讓該形式將變量發送到php腳本。

也許這適合您的情況,但總的來說應該如此。

<form name="hidden" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="addone" value="<script type=\"text/javascript\">addOne();</script>" />
 <input type="submit" value="Submit" />
 </form>

和你的PHP代碼; 添加此行:

$currentbidget=$_POST['addone'];

您遇到范圍問題,除非訪問全局,否則無法從功能內更改當前出價

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM