What are the potential pitfalls of using JavaScript to set focus on form fields in PHP applications?

When using JavaScript to set focus on form fields in PHP applications, a potential pitfall is that users with JavaScript disabled will not benefit from this functionality. To ensure accessibility and usability for all users, it is important to provide a fallback solution using PHP to set focus on form fields when JavaScript is disabled.

<?php
// Check if JavaScript is enabled
echo '<noscript>';
echo '<style>.js-only { display: none; }</style>';
echo '</noscript>';

// PHP fallback to set focus on form field
if (!isset($_GET['js_disabled'])) {
    echo '<script>window.location.href = "example.php?js_disabled=true";</script>';
} else {
    echo '<input type="text" name="username" id="username" autofocus>';
}
?>