我在想办法将javascripts值传递到.php脚本,然后从那里传递到.txt脚本时遇到了一些困难。
当用一个正则数字做这件事时,它是有效的,但当我想用它必须是的变量做这件事情时,.txt文件是空白的,并且没有向文件中添加任何内容。我在网上搜索了很多选项,但我不知道如何让它发挥作用。目前我的脚本如下和
我相信这是正确的方法,正如我所说,它只是有一些问题,正如我也说过的,我认为错误在于从javascript文件到.php文件的部分。
javascript/html/
<form id="pg-form" action="chargeCard.php" method="POST" name="pg-form">
<input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" />
<input type="hidden" id="curValueField" value=""/> <!-- this is where I am trying to pass the value -->
<input type="image" src="pgBut1.png" id="pgBut" value="submit" alt="butPG"/>
</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">
window.onload(function(){
var currentVarValue = 1; //this is the variable value I am trying to pass
document.getElementById("currentVarValue").innerHTML = currentVarValue;
document.getElementById("curValueField").value = currentVarValue; //this is where I get the variable value and make the new id to get in the html form
});
</script>
php:
<?php
require_once('./stripe-php/init.php');
'Stripe'Stripe::setApiKey("removed for security");
$token = $_POST['stripeToken'];
$myAmount = $_POST['amount'];
$describtion = $_POST['description'];
$curValue= $_POST['curValueField']; //this is where I try to get the variable value
$myAmount = round((int)$myAmount*100,0);
try {
$charge = 'Stripe'Charge::create(array(
"amount" => $myAmount,
"currency" => "usd",
"source" => $token,
"description" => $describtion));
//pass value to .txt file start
$filename = "getVarValue.txt";
$content = file_get_contents($filename);
$content .= $curValue;
file_put_contents($filename, $content);
//pass value to .txt file end
} catch('Stripe'Error'Card $e) {
}
?>
您的curValueField没有名称,因此它的值永远不会发送到您的PHP。
试试这个:
<input type="hidden" id="curValueField" name="curValueField" value=""/>
我想你忘了"name",尝试
<input type="hidden" id="curValueField" name="curValueField" value=""/>
并且到.txt
,可以试试下面的代码
$fp = fopen("test.txt", "a");
if($fp) {
fwrite($fp,$msg);
}
fclose($fp);