dsbtwy1329 2015-07-01 09:37
浏览 961
已采纳

如何将HTML格式的表单数据保存为txt文件。

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.

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.

Here is an example of the txt file data:

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:

enter image description here

What do I do to update the original txt file by pressing the submit button?

  • 写回答

2条回答 默认 最新

  • dousong1992 2015-07-01 09:45
    关注

    Tested, works 100%. You don't have to create .txt. Gets created automatically if not present.

    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
        $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"]));
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题