[英]AJAX/ Javascript - Pass contents of txt file into Javascript variable
[英]Pass variable to .txt file
我试图将变量从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) {
}
?>
您对文件不做任何事情。
您需要打开文件并将该值写入其中。
$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
});
您必须实际打开文件并将新数据写入其中。 您要做的只是使用文件名设置本地变量,不打开它,也不获取内容。 首先需要阅读http://php.net/manual/en/function.fopen.php 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.