【问题标题】:Adding to variable through php won't work通过php添加到变量将不起作用
【发布时间】:2015-03-25 21:18:33
【问题描述】:

我有一个 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 '&lt;script type="text/javascript"&gt;addOne();&lt;/script&gt;"'; 这一行

【问题讨论】:

  • 按 f12 并重新加载页面。控制台中的任何错误? php 脚本与 product1.html 的关系有多远?你的php脚本是怎么调用的?
  • 我可能认为这是问题所在,因为它与我用来调用 chargin 函数的脚本相同,但我没有做任何事情来将脚本与 javascript 函数连接起来,这可能是它不起作用的原因吗?
  • PHP 是如何进入页面的......?这两个文件是分开调用的吗? -- 在这种情况下它永远不会起作用。
  • 是的,它们是两个独立的文件。

标签: javascript php html variables addition


【解决方案1】:

我第一眼看到的只是一个语法错误:

你的双引号太多了( " )。

会是这样吗?

修正后的回声是:

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

【讨论】:

  • 我刚试过,还是不行,谢谢你的回答。
  • 嗨,我认为 php 中的回声可能发生在 DOM 中加载元素“currentBid”之前。你有任何 JavaScript 错误吗? (f12)
  • 不,我正在 EasyPHP 中对其进行测试,所以我认为不会显示 javascript 错误。
  • 你认为是因为我加载页面时由于没有保存而导致金额重置吗?
  • 确切地说,您不能在分别运行一个服务器端和一个客户端的 2 个文件之间交换变量,该脚本必须作为一个运行。你需要一种不同的方法。 Justin Vandehey 的回答为您提供了使用 PHP 会话的选项。或者,您也可以使用 cookie。如果我有时间我也会发布一个解决方案......现在我有点忙......
【解决方案2】:

看起来您正试图将 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: Sessions

Session 解决方案可能会做更多工作,但绝对值得!

【讨论】:

【解决方案3】:

关键概念:
1. php 在服务器端运行。 2. javascript在客户端运行

问题: 服务器没有由 javascript 设置的变量,因为它是在客户端设置的。

方法: 在您的 php 能够使用来自 javascript 的变量处理脚本之前,您的 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'];

【讨论】:

  • 我认为value="&lt;script type="text/javascript"&gt;addOne();&lt;/script&gt;" /&gt; 行可能有问题,因为它不起作用,但感谢您的回答。
  • 糟糕,我看到了自己的错误。我从action= 中删除了echo,因为它应该读作action="&lt;? $_SERVER['PHP_SELF']; ?&gt;,我们应该在javascript 标记内转义"。已经编辑了我的答案。
  • 我已经编辑了脚本,但它仍然不起作用,我认为这是因为我的“发布”到 php 脚本的形式有点不同,因为这是收费的工作方式,但是我做了两个馅饼,所以你可以看看它的样子。 php:pastie.org/private/lcs7fmklhhw2oaxnmixn8w html:pastie.org/private/7hbef8g6f6ddgytzwhtcg
【解决方案4】:

您遇到了范围问题,除非访问全局,否则无法从函数内更改当前出价

【讨论】:

  • 你的意思是我应该从 html 脚本访问 php 脚本,反之亦然?
猜你喜欢
  • 2015-04-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2020-03-27
相关资源
最近更新 更多