1

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?

Shant Cancik
  • 113
  • 1
  • 2
  • 9
  • a smilar question is asked in the below link. http://stackoverflow.com/questions/14998961/php-write-file-from-input-to-txt – Pierre Irani Jul 01 '15 at 09:42

2 Answers2

7

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"]));
?>
  • Thanks for this! For some reason when I use you're 4th line and sett the file to be opened as configurationSettings.txt it does not do anything. – Shant Cancik Jul 01 '15 at 10:38
  • I changed it to test.txt and it worked but I need it to rewrite the original configurationSettings.txt file. Any idea on what the problem is? – Shant Cancik Jul 01 '15 at 10:39
  • edited my answer. you have to use an other open parameter in order to read & write. check this: http://us2.php.net/manual/en/function.fopen.php#refsect1-function.fopen-parameters –  Jul 01 '15 at 10:44
  • This is so strange. Even with your edit. It still won't work if I used configurationSettings.txt. It makes no change to the file. If I label it as anything else it creates it and works correctly. I'm not sure what is wrong – Shant Cancik Jul 01 '15 at 13:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82097/discussion-between-maurize-and-shant-cancik). –  Jul 01 '15 at 13:54
0
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";
}}
user219839
  • 11
  • 1
  • While answers are always appreciated, this question was asked 2 **years** ago, and already had an accepted solution. Please try to avoid 'bumping' questions to the top by providing answers to them, unless the question was not already marked as resolved, or you found a new and improved solution to the problem. Also remember to provide some [**context surrounding your code**](https://meta.stackexchange.com/questions/114762) to help explain it. Check out the documentation on [**writing great answers**](http://stackoverflow.com/help/how-to-answer) for some tips on how to make your answers count :) – Obsidian Age Aug 23 '17 at 02:11