Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Enhancing a Form Specifying Required Fields

I need some explanation

Can someone explain to me this if statement line by line please ?

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));

$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));

$category = trim(filter_input(INPUT_POST,"category",FILTER_SANITIZE_STRING));

$title = trim(filter_input(INPUT_POST,"title",FILTER_SANITIZE_STRING));

$format = trim(filter_input(INPUT_POST,"format",FILTER_SANITIZE_STRING));

$genre = trim(filter_input(INPUT_POST,"genre",FILTER_SANITIZE_STRING));

$year = trim(filter_input(INPUT_POST,"year",FILTER_SANITIZE_STRING));

$details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

1 Answer

I've commented line by line and separated out the filter_input function onto multiple lines explaining what everything is for. I've removed duplicates where FILTER_SANITIZE_STRING is being used because it's the same but targeting a different form field. Hope it helps!

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { // Check to see if a form was submitted specifically using method="post" (as opposed to get which turns fields into query strings), if true proceed to process inputs

$name = trim(
  filter_input(
    INPUT_POST, // This tells filter_input that we are looking for a form that was submitted with the method of post
    "name", // This is the name of the form field we are filtering
    FILTER_SANITIZE_STRING // These are the rules that we are filtering against. FILTER_SANITIZE_STRING performs: Strip tags, optionally strip or encode special characters.
  )
);

$email = trim(
  filter_input(
    INPUT_POST, // This tells filter_input that we are looking for a form that was submitted with the method of post
    "email", // This is the name of the form field we are filtering
    FILTER_SANITIZE_EMAIL // These are the rules that we are filtering against. FILTER_SANITIZE_EMAIL performs: Remove all characters except letters, digits and !#$%&'*+-=?^_`{|}~@.[].
  )
);


$details = trim(
  filter_input(
    INPUT_POST, // This tells filter_input that we are looking for a form that was submitted with the method of post
    "details", // This is the name of the form field we are filtering
    FILTER_SANITIZE_SPECIAL_CHARS // These are the rules that we are filtering against. FILTER_SANITIZE_SPECIAL_CHARS performs: HTML-escape '"<>& and characters with ASCII value less than 32, optionally strip or encode other special characters.
  )
);