【发布时间】:2015-04-01 16:45:46
【问题描述】:
我正在尝试将变量从 html 表单传递到 .php 脚本,然后从 .php 脚本传递到存储变量值的 .txt 文件中。问题是,当我测试脚本而不是发布变量编号时,它只是发布变量名称“numberVariable”。
html/javascript
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" />
<input type="hidden" id="numberVariable" name="numberVariable" value="numberVariable"/> <!--this is where I try to pass the variable to the .php script-->
<input type="image" src="Button1.png" id="customButton" value="pay" alt="button"/>
</form>
<script type="text/javascript">
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<script type="text/javascript">
var numberVariable= 1; //this is the variable I am trying to pass to the .php script to then pass the value to the .txt file
document.getElementById("numberVariable").innerHTML = numberVariable;
document.getElementByID("numberVariable").value = numberVariable;
</script>
php
<?php
require_once('./stripe-php/init.php');
\Stripe\Stripe::setApiKey("removed for safety");
$token = $_POST['stripeToken'];
$myAmount = $_POST['amount'];
$describtion = $_POST['description'];
$numberVariable= $_POST['numberVariable']; //this is where I get the variable from the form
$myAmount = round((int)$myAmount*100,0);
try {
$charge = \Stripe\Charge::create(array(
"amount" => $myAmount,
"currency" => "usd",
"source" => $token,
"description" => $describtion));
//PASTE VARIABLE TO .TXT FILE START
$filename = "iPhoneAuctionBids.txt";
$content = file_get_contents($filename);
$content .= $numberVariable. PHP_EOL;
file_put_contents($filename, $content);
//PASTE VARIABLE TO .TXT FILE END
} catch(\Stripe\Error\Card $e) {
}
?>
【问题讨论】:
-
首先你必须在html元素之后移动javascript代码。因为在执行它们时,numberVariable 不存在。
-
谢谢你的信息,我已经按照你说的做了。
标签: javascript php html text-files