Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将变量传递到.txt文件_Javascript_Php_Html_Text Files - Fatal编程技术网

Javascript 将变量传递到.txt文件

Javascript 将变量传递到.txt文件,javascript,php,html,text-files,Javascript,Php,Html,Text Files,我试图将一个变量从html表单传递到.php脚本,然后从.php脚本传递到存储变量值的.txt文件中。问题是,当我测试脚本而不是发布变量号时,它只发布变量名“numberVariable” html/javascript 函数isNumberKey(evt) { var charCode=(evt.which)?evt.which:event.keyCode 如果(字符码>31&(字符码57)) 返回false; 返回true; } var numberVariable=1//这是我试图传递给

我试图将一个变量从html表单传递到.php脚本,然后从.php脚本传递到存储变量值的.txt文件中。问题是,当我测试脚本而不是发布变量号时,它只发布变量名“numberVariable”

html/javascript

函数isNumberKey(evt)
{
var charCode=(evt.which)?evt.which:event.keyCode
如果(字符码>31&(字符码<48 | |字符码>57))
返回false;
返回true;
}
var numberVariable=1//这是我试图传递给.php脚本的变量,然后将值传递给.txt文件
document.getElementById(“numberVariable”).innerHTML=numberVariable;
document.getElementByID(“numberVariable”).value=numberVariable;
php


您必须实际打开文件并将新数据写入其中。您所要做的就是使用文件名设置一个局部变量,而不是打开它,也不是获取内容。是您首先需要阅读的内容。

您不需要对该文件执行任何操作

您需要打开该文件并将该值写入其中

$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
});

首先,必须将javascript代码移到html元素之后。因为当它们被执行时,numberVariable不存在。谢谢你提供的信息,我已经按照你说的做了。谢谢你的回答,我已经编辑了我的脚本,就像下面的链接一样,但是现在提交时,它工作了,但它只写变量“numberVariable”的名称,而不是数字你能给你的JavaScript发一个链接吗?我试着编辑JavaScript,使其与以下链接类似,但问题是它应该可以工作,而不需要在输入字段中输入值,然后在JavaScript函数外部赋值,但它对我不起作用,但如果我选择在值点中输入1,它就可以工作,因此,我将变量传递给.php php:javscript的方式中肯定存在一些错误:正如我在回答中所写的,您需要将JS封装在window.onload中,否则输入可能不存在,您无法在其中输入值。比如:window.onload(function(){document.getElementById(“numberVariable”).value=numberVariable;})。你不知道;I don’我不需要输入域的innerHTML部分。非常感谢您的回答,您的做法是正确的,我刚刚编辑了一点。
$filename = "iPhoneAuctionBids.txt"; 
$content = file_get_contents($filename);
$content .= $numberVariable . PHP_EOL;
file_put_contents($filename, $content);
window.onload(function(){
// here
});