我正在尝试将变量从html表单传递到.php脚本,然后从.php脚本传递到存储变量值的.txt文件中。问题是,当我测试脚本而不是发布变量号时,它只是发布变量名称" numberVariable"。
<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
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) {
}
?>
答案 0 :(得分:2)
你不对文件做任何事情。
您需要打开文件并将该值写入其中。
$filename = "iPhoneAuctionBids.txt";
$content = file_get_contents($filename);
$content .= $numberVariable . PHP_EOL;
file_put_contents($filename, $content);
此外,您可能希望在对DOM执行任何js处理之前等待生成html。把你的JS代码放在
中window.onload(function(){
// here
});
答案 1 :(得分:0)
您必须实际打开文件并将新数据写入其中。您所要做的就是使用文件名设置本地变量,而不是打开它,也不是获取内容。 http://php.net/manual/en/function.fopen.php是您首先需要阅读的内容。