How can the PHP_SELF variable be properly used in a form action attribute to ensure correct form submission handling?

When using the PHP_SELF variable in a form action attribute, it is important to sanitize the input to prevent potential security vulnerabilities such as cross-site scripting attacks. One way to ensure correct form submission handling is to use htmlspecialchars() function to escape special characters in the variable before using it in the form action attribute. This will help prevent malicious code injection and ensure that the form data is submitted securely.

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  <!-- Form fields go here -->
</form>