don't click here

Newbie PHP question.

Discussion in 'Technical Discussion' started by Jeffery Mewtamer, Oct 16, 2019.

  1. Jeffery Mewtamer

    Jeffery Mewtamer

    Blind Bookworm Member
    1,886
    94
    28
    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):
    1.  
    2. <form action="save.php" method="post">
    3. <input type="radio" name="option" value="foo">Foo<br>
    4. <input type="radio" name="option" value="bar">Bar<br>
    5. <input type="radio" name="option" value="bas">Bas<br>
    6. Label:
    7. <input type="text" name="label"><br>
    8. Comment:
    9. <input type="text" name="comment"><br>
    10. <input type="submit" value="Submit">
    11. </form>
    12.  
    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).
     
  2. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,239
    972
    93
    Long-term happiness
    I see you're doing this as a POST request, soooo.... Stick this at the top of your save.php.

    Code (Text):
    1. <?php
    2.  
    3. $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
    4. $labelValue == "";
    5. $commentValue == "";
    6.  
    7. if (isset($_POST['option'])) {   //If the option value is set
    8.     $optionValue == $_POST['option']);  //Set the contents of the option value to the variable $optionValue
    9.     $labelValue == $_POST['label']);  //Set the contents of the option value to the variable $labelValue
    10.     $commentValue == $_POST['comment']);  //Set the contents of the option value to the variable $commentValue
    11.  
    12.    $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.
    13.    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.
    14.    fclose($dataFileWrite);      //Done with the file, close it
    15.  
    16.    echo("Appended the following: labelValue = ".$labelValue."<br>commentValue = ".$commentValue."<p>To the following text file: ".$optionValue.".txt");
    17. }
    18. else {
    19.    echo("No data detected!");
    20. }
    21. ?>
    22.  
    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
     
    Last edited: Oct 18, 2019
  3. Billy

    Billy

    RIP Oderus Urungus Member
    2,119
    179
    43
    Colorado, USA
    Indie games
    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.
     
  4. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,239
    972
    93
    Long-term happiness
    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.
     
  5. Billy

    Billy

    RIP Oderus Urungus Member
    2,119
    179
    43
    Colorado, USA
    Indie games
    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.
     
  6. Neo Geo MVS

    Neo Geo MVS

    Giga Power Pro-Gear Spec Oldbie
    37
    11
    8
    Looks good, but you'll need to switch the assignment order on lines 8, 9, and 10:
    Code (Text):
    1. $optionValue = $_POST['option'];
    2. $labelValue = $_POST['label'];
    3. $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):
    1. if ($optionValue != 'foo' && $optionValue != 'bar' && $optionValue != 'bas') die("Invalid file!");
     
  7. Overlord

    Overlord

    Now playable in Smash Bros Ultimate Moderator
    19,239
    972
    93
    Long-term happiness
    Doh, how the fuck did I miss that? You are of course correct. Edited accordingly.
     
  8. Jeffery Mewtamer

    Jeffery Mewtamer

    Blind Bookworm Member
    1,886
    94
    28
    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.