How can a self-referencing form be implemented in PHP to save user input without redirecting to a different page?

To implement a self-referencing form in PHP to save user input without redirecting to a different page, you can use the $_SERVER['PHP_SELF'] variable in the form action attribute. This variable refers to the current script, allowing the form to submit to itself. You can then process the form data on the same page without redirection.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data here
    $input_data = $_POST['input_field'];
    // Save data to database or perform any necessary actions
}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="input_field">
    <button type="submit">Submit</button>
</form>