Entering text and saving it to a server file

Saturday 30 April 2022

What I would like to do today is to be able to enter text on a web page and have it entered into a server file. That is complicated by the need to validate the entry so a page can’t be hacked. Yesterday, or maybe it was the day before, I think I figure out in general what I needed to do, and even wrote/found code snippets to do it. Today I want to try it and see if it works.

First, I need to remember how to get MAMP running, so I can do the trial and error on my local machine. OK, that is just a matter of finding
1. MAMP (which is in my applications launch page), and
2. the MAMP htdocs directory (there’s a shortcut in my favorites panel)
OK, so I can launch index, and I can see it is running on local host (and I have a note on that instance of the index that says “MAMP” as a double check), so that is good, Clicking on subsidiary pages works fine, and they too run on local host.

BUT when I click on the .php link firefox wants to open the file with something else, rather than run the page.
…I thought this was working before.
OK, looks like I left workspace.php in a non-working form.
…but restoring the original doesn’t work. It still opens in BBEdit rather than running.
…I thought that I had this working.
Ug.

Views: 9

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