通过JavaScript设置的表单字段值不会作为$\u POST的一部分传递给PHP脚本
我在想办法将javascripts值传递给.php脚本,然后再传递给.txt脚本时遇到了一些麻烦通过JavaScript设置的表单字段值不会作为$\u POST的一部分传递给PHP脚本,javascript,php,html,forms,post,Javascript,Php,Html,Forms,Post,我在想办法将javascripts值传递给.php脚本,然后再传递给.txt脚本时遇到了一些麻烦 当使用常规数字时,它可以工作,但当我想使用必须使用的变量时,.txt文件保留为空,并且没有添加任何内容。我已经在网上搜索了“干燥”的选项,但我就是不知道如何让它工作。目前我的脚本如下所示 我相信这是正确的做法,正如我所说,只是有一些问题,正如我所说的,我相信错误在于从javascript文件到.php文件的部分 javascript/html: 函数isNumberKey(evt) { var c
当使用常规数字时,它可以工作,但当我想使用必须使用的变量时,.txt文件保留为空,并且没有添加任何内容。我已经在网上搜索了“干燥”的选项,但我就是不知道如何让它工作。目前我的脚本如下所示 我相信这是正确的做法,正如我所说,只是有一些问题,正如我所说的,我相信错误在于从javascript文件到.php文件的部分 javascript/html:
函数isNumberKey(evt)
{
var charCode=(evt.which)?evt.which:event.keyCode
如果(字符码>31&(字符码<48 | |字符码>57))
返回false;
返回true;
}
window.onload(函数(){
var currentVarValue=1;//这是我试图传递的变量值
document.getElementById(“currentVarValue”).innerHTML=currentVarValue;
document.getElementById(“CurveValueField”).value=currentVarValue;//这是获取变量值并在html表单中生成新id的地方
});
php:
您的CurveValueField没有名称,因此其值永远不会发送到PHP 试试这个:
<input type="hidden" id="curValueField" name="curValueField" value=""/>
我想你忘了“命名”,
试一试
这已经有人提出了。OP的文件编写代码看起来还可以,不是吗?“当使用常规数字执行此操作时,它可以工作”看起来可以工作。但是这对OP来说可能很有趣。谢谢你的建议,我已经解决了这个问题。谢谢你的回答,我已经找到了解决方法。
<?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) {
}
?>
<input type="hidden" id="curValueField" name="curValueField" value=""/>
<input type="hidden" id="curValueField" name="curValueField" value=""/>
$fp = fopen("test.txt", "a");
if($fp) {
fwrite($fp,$msg);
}
fclose($fp);