What are potential security risks associated with using PHP_SELF in form actions and how can they be mitigated?

Using PHP_SELF in form actions can lead to potential security risks such as cross-site scripting (XSS) attacks and form spoofing. To mitigate these risks, it is recommended to use htmlspecialchars() function to sanitize the input before using it in the form action attribute.

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