通过 .php 将变量值发送到 .txt 文件
Send variable value to .txt file through .php
我有一个带有变量的脚本,我将这个变量传递给 .php 脚本,然后从 .php 脚本传递给 .txt 文件,然后存储数字值变量,唯一的问题是,当我将变量传递给 .txt 文件时,不是传递变量的编号,而是传递变量的名称。
这是我的脚本:
php:
<?php
$variableToPass= $_POST['variableToPass'];
$filename = "textFilePass.txt";
$content = file_get_contents($filename);
$content .= $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
?>
html:
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
<input type="image" src="butImg.png" id="Button" value="but" alt="but"/>
</form>
<script type="text/javascript">
var variableToPass= 1;
document.getElementById("variableToPass").innerHTML = variableToPass;
document.getElementByID("variableToPass").value = variableToPass;
</script>
你这里有错字:
document.getElementByID("variableToPass").value = variableToPass;
^
改为:
document.getElementById("variableToPass").value = variableToPass;
该值未更改并保持默认值,即 variableToPass
,与 PHP 变量名称一致。
此外,您不需要更改 innerHTML - 它是输入,它有其价值。
试试这个:
$var_str = var_export($text, true);
$var = "<?php\n\n$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
这将始终发送值 (value="variableToPass") 尝试将其更改为 (value="1") 这应该 return 1
"It is because I want to save the variable to be able to show it to all users on the website, so to do this I have the default value which is 1 and then I have a text input, which only allows numbers which are the variable+1 so lets say the default is 1, then I will put 2 into the input and then the .php file will get this input of 2 and save it into the .txt file, where after that the variable in the javascript will get this variable in the .txt file, and turn the new value into the variable. A bit complicated but basically it is just to save the variable for all users and not locally."
好的,我很清楚你想在这里做什么。
重要旁注:请仔细完整地阅读我的回答;我可能没有完全理解你想在这里实现的目标,所以我的回答有两个选项。
这是您需要做的:
首先使用is_numeric()
检查输入是否为数字。
然后分配变量并将POST数量与$_POST['variableToPass']
数组相加。
PHP
<?php
if(isset($_POST['amount']) && is_numeric($_POST['amount']) ){
$amount = $_POST['amount'];
}
else{
echo "It is not numeric. Please click back and enter an integer.";
exit;
}
$variableToPass1 = $_POST['variableToPass'];
$variableToPass = $variableToPass1 + $amount;
$filename = "textFilePass.txt"; // make sure this file exists before executing
$content = file_get_contents($filename);
$content .= $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
HTML形式
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
<input type="submit" name="submit" value="Submit"/>
</form>
<script type="text/javascript">
var variableToPass= 1;
document.getElementById("variableToPass").innerHTML = variableToPass;
document.getElementById("variableToPass").value = variableToPass;
</script>
但是,使用此方法将 adding/appending 保存到文件中。如果这不是您想要的结果,您将需要删除以下点:
$content .= $variableToPass. PHP_EOL;
^
读作:
$content = $variableToPass. PHP_EOL;
另外,如前所述;这一行:
document.getElementByID("variableToPass").value = variableToPass;
应读作:
document.getElementById("variableToPass").value = variableToPass;
- getElementById 区分大小写。
选项 2 使用 "amount" 输入的数字写入文件。
这将从输入中写入任何数字并将其写入文件,而不是附加到它。
如果没有插入数字,仅通过单击提交按钮,将写入您在 var variableToPass= 1;
中定义的数字
<?php
if(isset($_POST['amount']) && is_numeric($_POST['amount']) ){
$variableToPass = $_POST['amount'];
}
else{
$variableToPass = $_POST['variableToPass'];
}
$filename = "textFilePass.txt";
$content = file_get_contents($filename);
$content = $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
脚注:
- 如果您对此有任何疑问,或者我可能没有完全理解;让我知道。
我有一个带有变量的脚本,我将这个变量传递给 .php 脚本,然后从 .php 脚本传递给 .txt 文件,然后存储数字值变量,唯一的问题是,当我将变量传递给 .txt 文件时,不是传递变量的编号,而是传递变量的名称。
这是我的脚本:
php:
<?php
$variableToPass= $_POST['variableToPass'];
$filename = "textFilePass.txt";
$content = file_get_contents($filename);
$content .= $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
?>
html:
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
<input type="image" src="butImg.png" id="Button" value="but" alt="but"/>
</form>
<script type="text/javascript">
var variableToPass= 1;
document.getElementById("variableToPass").innerHTML = variableToPass;
document.getElementByID("variableToPass").value = variableToPass;
</script>
你这里有错字:
document.getElementByID("variableToPass").value = variableToPass;
^
改为:
document.getElementById("variableToPass").value = variableToPass;
该值未更改并保持默认值,即 variableToPass
,与 PHP 变量名称一致。
此外,您不需要更改 innerHTML - 它是输入,它有其价值。
试试这个:
$var_str = var_export($text, true);
$var = "<?php\n\n$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
这将始终发送值 (value="variableToPass") 尝试将其更改为 (value="1") 这应该 return 1
"It is because I want to save the variable to be able to show it to all users on the website, so to do this I have the default value which is 1 and then I have a text input, which only allows numbers which are the variable+1 so lets say the default is 1, then I will put 2 into the input and then the .php file will get this input of 2 and save it into the .txt file, where after that the variable in the javascript will get this variable in the .txt file, and turn the new value into the variable. A bit complicated but basically it is just to save the variable for all users and not locally."
好的,我很清楚你想在这里做什么。
重要旁注:请仔细完整地阅读我的回答;我可能没有完全理解你想在这里实现的目标,所以我的回答有两个选项。
这是您需要做的:
首先使用is_numeric()
检查输入是否为数字。
然后分配变量并将POST数量与$_POST['variableToPass']
数组相加。
PHP
<?php
if(isset($_POST['amount']) && is_numeric($_POST['amount']) ){
$amount = $_POST['amount'];
}
else{
echo "It is not numeric. Please click back and enter an integer.";
exit;
}
$variableToPass1 = $_POST['variableToPass'];
$variableToPass = $variableToPass1 + $amount;
$filename = "textFilePass.txt"; // make sure this file exists before executing
$content = file_get_contents($filename);
$content .= $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
HTML形式
<form id="payment-form" action="chargeCard.php" method="POST" name="payment-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="variableToPass" name="variableToPass" value="variableToPass"/>
<input type="submit" name="submit" value="Submit"/>
</form>
<script type="text/javascript">
var variableToPass= 1;
document.getElementById("variableToPass").innerHTML = variableToPass;
document.getElementById("variableToPass").value = variableToPass;
</script>
但是,使用此方法将 adding/appending 保存到文件中。如果这不是您想要的结果,您将需要删除以下点:
$content .= $variableToPass. PHP_EOL;
^
读作:
$content = $variableToPass. PHP_EOL;
另外,如前所述;这一行:
document.getElementByID("variableToPass").value = variableToPass;
应读作:
document.getElementById("variableToPass").value = variableToPass;
- getElementById 区分大小写。
选项 2 使用 "amount" 输入的数字写入文件。
这将从输入中写入任何数字并将其写入文件,而不是附加到它。
如果没有插入数字,仅通过单击提交按钮,将写入您在 var variableToPass= 1;
<?php
if(isset($_POST['amount']) && is_numeric($_POST['amount']) ){
$variableToPass = $_POST['amount'];
}
else{
$variableToPass = $_POST['variableToPass'];
}
$filename = "textFilePass.txt";
$content = file_get_contents($filename);
$content = $variableToPass. PHP_EOL;
file_put_contents($filename, $content);
脚注:
- 如果您对此有任何疑问,或者我可能没有完全理解;让我知道。