通过php添加到变量won'不起作用
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>
php脚本,带有名为chargeCard.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:会话
会话解决方案可能会做更多的工作,但绝对值得!
关键概念:
1.php在服务器端运行。2.javascript在客户端上运行
问题:服务器并没有javascript设置的变量,因为它是在客户端设置的。
方法:您的javascript需要将变量发送到服务器,然后php才能使用javascript中的variable处理脚本。
方法:使用隐藏表单并让这个表单将变量发送到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'];
您有一个范围问题,除非访问全局
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- JsFiddle上的鼠标事件不起作用
- 我的AngularJS表达式没有'不起作用
- 点击按钮输入不起作用
- 面向对象的Javascript代码在IE7中不起作用
- 分部隐藏在jquery中不起作用
- 在phonegap应用程序内部重定向不起作用
- Array.length似乎不起作用;console.log则显示其他情况
- $ionicplatform内的$scope不;不起作用
- AngularJS ng include won'不知什么原因不起作用
- 通过php添加到变量won'不起作用
- 在html中显示javascript变量won'不起作用
- Javascript发布到表单-won'如果字段是隐藏的,则不起作用
- 在momentJS上设置区域设置won'不起作用
- angular js ng html in ng repeat won'不起作用