What are some best practices for handling form submissions in PHP to avoid errors like "Error 404 (File not found)"?

When handling form submissions in PHP, it is important to ensure that the form action points to the correct file or URL to avoid errors like "Error 404 (File not found)". One best practice is to use the PHP_SELF variable in the form action attribute to ensure that the form is submitted to the same PHP file that is processing the form data.

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
  // form fields here
  <input type="submit" name="submit" value="Submit">
</form>