How can the use of $_SERVER['PHP_SELF'] in PHP forms make scripts vulnerable to XSS attacks, and what alternative options are available for form actions?

Using $_SERVER['PHP_SELF'] in PHP forms can make scripts vulnerable to XSS attacks because it allows an attacker to inject malicious code into the form action URL. To prevent this vulnerability, it is recommended to use htmlspecialchars() function to encode the form action URL.

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