Okay, so I've been dusting off my HTML skills recently, and I think I've got the hang of creating HTML forms once more, but I've realized I haven't got a clue how to do anything with the information gathered by a form. I know php is involved, and I've been able to find some examples of using php to do simple manipulations on data from simple forms, but I haven't been able to follow the examples well enough to adapt them to my use case(probably doesn't help that I'm not familiar with php syntax or that my screen reader doesn't read most punctuation, which makes code harder to follow). For starters, I'd like to be able to append data from a form to a text file that I can later manually process, feed to a bash script or feed to a C++ program I wrote, but I'm interested in learning more of PHP as time goes on. To give a more concrete example, her's the HTML for a sample form: Code (Text): <form action="save.php" method="post"> <input type="radio" name="option" value="foo">Foo<br> <input type="radio" name="option" value="bar">Bar<br> <input type="radio" name="option" value="bas">Bas<br> Label: <input type="text" name="label"><br> Comment: <input type="text" name="comment"><br> <input type="submit" value="Submit"> </form> Ideally, I'd like to be able to append the contents of label and comment to the end of either foo.txt, bar.txt, or bas.txt depending on which radio button is selected when submit is activated. Any help on this specific task or recommendation of a good, text-only or audio beginner's guide to PHP would be much appreciated(and even just an explanation of how punctuation plays into PHP syntax would be helpful in trying to read code snippets).
I see you're doing this as a POST request, soooo.... Stick this at the top of your save.php. Code (Text): <?php $optionValue == ""; //Honestly not sure if we need to declare this ahead of time but just to make sure it's not in a loop below only, or something $labelValue == ""; $commentValue == ""; if (isset($_POST['option'])) { //If the option value is set $optionValue == $_POST['option']); //Set the contents of the option value to the variable $optionValue $labelValue == $_POST['label']); //Set the contents of the option value to the variable $labelValue $commentValue == $_POST['comment']); //Set the contents of the option value to the variable $commentValue $dataFileWrite = fopen($optionValue.".txt", "a") or die("Unable to open file!"); //This prepares our file, foo bar or bas.txt, for being appended to. You use r to read from it, and w to write to it without appending. fwrite($dataFileWrite, $labelValue.$commentValue); //Append the label and comment values to the file here. I've just rammed them both together (the . joins the two strings), you could put a space or something between them by say doing ." ". instead. Remember the . joins strings, the only difference here is there's a string of a space between the other two. fclose($dataFileWrite); //Done with the file, close it echo("Appended the following: labelValue = ".$labelValue."<br>commentValue = ".$commentValue."<p>To the following text file: ".$optionValue.".txt"); } else { echo("No data detected!"); } ?> I think this should work, I had a look at the syntax and it seems OK. I have no idea how readable this will be with your screenreader, I apologise in advance =P
It's been a while since I touched any PHP, but if I remember correctly, PHP puts all the values from the form into a string-indexed array, called $_POST. That is, dollar sign, underscore, and then "POST" in all caps. The index, I believe, comes from the "name" field of the HTML tag, so having all of them named "option" would not work. However, assuming you did have a radio button named option, you would capture its value like this: $_POST["option"]. That is, dollar sign, underscore, "POST" in all caps, and then a left bracket, the name "option" in quotation marks, and then a right bracket. Confusingly, PHP allows arrays to be indexed by string or numeric, and you can even mix them in the same array. So to append two values together, you would do a simple string append with a plus mark between the two values in the $_POST array, like this: $_POST["label"] + $_POST["option"] Please note that PHP does no filtering to the data, so the use of a built-in function like htmlspecialchars() ("html special chars" as all one word, all lowercase... thanks PHP) is recommended. Again, it's been a while since I touched PHP, so I apologize of some or all of this is wrong. Honestly when you move onto bigger projects I'd recommend staying away from PHP and use anything more up to date on the back-end. In my professional career I've mostly used ASP.NET MVC, but there's a lot of frameworks out there in almost any programming language you can think of.
PHP is free, Microsoft web servers aren't. As much stick as it gets for being a "toy language", a lot of the world's biggest websites use it. Also I checked elsewhere and I'm pretty sure he has his radio buttons set correctly.
Just to be clear, I wasn't advocating for ASP.NET specifically, even though .NET Core does run on Linux. My issues with PHP come down to it being a weirdly inconsistent programming language. But everyone has preferences and PHP gets the job done, so I'll drop it now and stop derailing this thread.
Looks good, but you'll need to switch the assignment order on lines 8, 9, and 10: Code (Text): $optionValue = $_POST['option']; $labelValue = $_POST['label']; $commentValue = $_POST['comment']; Also, if this will be on a public web server, I recommend adding this line after line 10 for security: Code (Text): if ($optionValue != 'foo' && $optionValue != 'bar' && $optionValue != 'bas') die("Invalid file!");
Okay, took some fiddling around and fighting with less than helpful parser error messages and took me a while to notice the == screwing up assignment statements, but I think I've got the hang of at least declaring variables, string concatenation, accessing data from a posted form, and printing messages to the screen. Apparently, I need to figure out how to setup a testing environment if I want to test php code from a local folder instead of doing testing on server. Also, it would appear any attempt to access a txt file will fail if the file doesn't already exist and already has sufficient permissions. Whether that's default behavior of the version of php that my web space came with or just how Apache came configured, I have no clue. Anyways, thanks for the help, and if I understand the code in this thread as well as I think I do, I suspect I'm covered for quite a few potential simple applications.