简体   繁体   English

如何将HTML中的表单数据保存到txt文件中。

[英]How do I save form data in HTML to a txt file.

My program uses PHP to open a list of configuration settings from a txt file called "configurationSettings.txt" and puts the data from it onto a form. 我的程序使用PHP从名为“ configurationSettings.txt”的txt文件中打开配置设置列表,并将其中的数据放入表单中。

What I'm trying to figure out is how to enable my program to update the data on the original txt file if the user changes anything through the form. 我要弄清楚的是,如果用户通过表单进行了任何更改,如何使我的程序能够更新原始txt文件上的数据。

Here is an example of the txt file data: 这是txt文件数据的示例:

Channel 7
4.0000
6.0000

Here is my code that reads the data and fills my form: 这是我的代码,它读取数据并填写表格:

<?php
$configFile = fopen("configurationSettings.txt", "r");
$title1 = fgets($configFile);
$gain1 = fgets($configFile);
$offset1 = fgets($configFile);
fclose($configFile);
?> 

<form action="program.php" method="post">
Channel 8 Title:<br>
<input type="text" name="channel0Title" value="<?php echo $title1 ?>">
<br>
Gain:<br>
<input type="text" name="channel0Gain" value="<?php echo $gain1 ?>">
<br>
Offset:<br>
<input type="text" name="Channel0Offset" value= "<?php echo $offset1 ?>">
<br>
<input type="submit" id ="submitButton" value="Submit">
</div>
</form>

And heres a picture of what it looks like: 这是它的外观图:

在此处输入图片说明

What do I do to update the original txt file by pressing the submit button? 如何通过按“提交”按钮来更新原始txt文件?

Tested, works 100%. 经过测试,可以100%工作。 You don't have to create .txt. 您不必创建.txt。 Gets created automatically if not present. 如果不存在,则自动创建。

index.html index.html

<form action="program.php" method="post">
    Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
    Gain:<br><input type="text" name="channel0Gain" value="4.000"><br>
    Offset:<br><input type="text" name="channel0Offset" value= "6.000"><br>
    <input type="submit" id ="submitButton" value="Submit">
</form>

program.php 程序.php

<?php
    $title = $_POST["channel0Title"]; //You have to get the form data
    $gain = $_POST["channel0Gain"];
    $offset = $_POST["channel0Offset"];
    $file = fopen('configurationSettings.txt', 'w+'); //Open your .txt file
    ftruncate($file, 0); //Clear the file to 0bit
    $content = $title. PHP_EOL .$gain. PHP_EOL .$offset;
    fwrite($file , $content); //Now lets write it in there
    fclose($file ); //Finally close our .txt
    die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die("There was an error writing this file");
}
else {
    echo "$ret bytes written to file";
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用php将html表单数据保存到txt文件 - Save html form data to a txt file using php 如何将表单数据保存到文本文件? - How do I save form data to a text file? 尝试将表单数据存储在文本文件中时,我的debian linux服务器中的lighttpd服务器出现403错误。 我该如何解决 - 403 error in lighttpd server in my debian linux server while trying to store form data in a text file. How do I solve this 如何将函数的结果保存到txt文件中? - How do I save the results of my function to a txt file? 如何使用 html 和 php 保存文件 txt - How to save file txt using html and php 我可以从HTML表单中读取输入并将其保存在TXT文件中的特定位置吗? - Can I read the input from a HTML form and save it at at a specific position in TXT file? PHP如何循环从html表单获取的数据并将其保存到mysql数据库中? - PHP how do I loop data which I got from html form and save them into mysql databse? 在Presatshop上:我制作了用于创建TXT文件的PHP页面。 在本地上可以,但是在服务器上不要写 - On Presatshop: i made PHP page for create TXT file. On local it works, but on server do not write 如何将数据从 HTML/PHP 表单保存到文本文件 - How to Save data from a HTML/PHP Form to a Text file 我怎样才能用 php 保存在 file.txt 上? - How could I save on a file .txt with php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM