Back to File Handling

Wednesday 27 April 2022

When I last wrote I was focused on trying to create an exercise tracking list with checkboxes for my workspace page. Since then I’ve decided it is easier and more useful to just use a spreadsheet, and so I did that and got distracted entirely from web programming by my physical therapy, etc., regime.

Now I want to take up programming again. I would like to return to the file-handling material again, so that I can enter text into a file (e.g., like my quotations file) from a web page. I would like to be able to do the following

  • Flashcards.
    Get flashcards working, drawing material from a file, and be able to create new flashcards by adding text to a file from the web page
  • Quotations. These now randomly select lines from a file.
    I would also like to be able to add new quotations by typing an entry into the web page, rather than editing a copy of the file on my mac and then uploading it.
  • For both of the above I would like to be able to add some state variables so that I can suppress an item, or cause it to show up more frequently (e.g., by clicking a button).

Getting Text Input with Forms

OK, I can get input like this:

<form action="/action_page.php">
  <label for="TextInput">Quotation</label><br>
  <input type="text" id="TextInput" name="TextInput"<br><br>
  <input type="submit" value="Submit">
</form> 

This passes an input “XXX” to “action_page.php” as TextInput=XXX.
That’s nice, but what I want to do is get that text into a file, presumably appended to a file.

OK, let’s try this again. Here’s code that gets the input

<html>
<body>

<form action="WriteToFile_get.php" method="get">
Quotation: <input type="text" name="EnteredQuotation"><br>
<input type="submit">
</form>

</body>
</html>

and then we have a WriteToFile_get.php file that is:

<html>
<body>

<?php echo $_GET["EnteredQuotation"]; ?><br>

</body>
</html>

I think, in the above two blocks, all instances of “get” could be changed to “post”

In either case, my working hypothesis is that $_GET[“<name value>”] will contain the entered data and should be able to be appended to a file.

So WritetoFile.php should be:

<html>
<body>

<?php echo $_GET["EnteredQuotation"]; ?><br>


<? php
$fh = fopen(“quotations.txt”, ‘a’) or die(“Failed to create text file.”);
fwrite($fh,$_GET["EnteredQuotation") or die(“Could not write to file.”);
fclose($fh);
echo “File ‘quotations.txt’ written successfully.”;
?>

</body>
</html>

OK, let’s modify this to (1) use PUT, (2) use validation. And once this is working, change the type from text to textarea (which apparently doesn’t require the “input” command, i.e.: <textarea name=”EnteredQuotation” rows=”5″ cols=”40″></textarea>

<html>
<body>

<form action="WriteToFile_get.php" method="post">
Quotation: <input type="text" name="EnteredQuotation"><br>
<input type="submit">
</form>

</body>
</html>

AND the WriteToFile.php page will be

<html>
<body>

<?php echo $_PUT["EnteredQuotation"]; ?><br>

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $EnteredQuotation = test_input($_POST["EnteredQuotation"]);
 }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

$fh = fopen(“quotations.txt”, ‘a’) or die(“Failed to create text file.”);
fwrite($fh,$_GET["EnteredQuotation") or die(“Could not write to file.”);
fclose($fh);
echo “File ‘quotations.txt’ written successfully.”;
?>

</body>
</html>


?>

Views: 2