How can the issue of disappearing content when selecting categories in a form be elegantly resolved in PHP?

Issue: The problem of disappearing content when selecting categories in a form can be elegantly resolved by using AJAX to dynamically update the form fields without refreshing the page.

<?php
// PHP code to handle form submission and dynamically update form fields using AJAX

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Handle form submission
    $selectedCategory = $_POST['category'];

    // Perform necessary actions based on selected category

    // Return updated form fields using JSON
    $response = array(
        'field1' => 'Updated content for field 1',
        'field2' => 'Updated content for field 2'
    );

    header('Content-Type: application/json');
    echo json_encode($response);
}
?>