What is the best practice for handling dependent dropdowns in PHP forms, especially when it involves variable pricing based on user selections?

When dealing with dependent dropdowns in PHP forms, especially when it involves variable pricing based on user selections, the best practice is to use AJAX to dynamically update the options in the second dropdown based on the selection in the first dropdown. This allows for a smoother user experience and ensures that the pricing is accurately reflected based on the user's selections.

// HTML form with two dropdowns
<form id="myForm">
    <select id="dropdown1" name="dropdown1">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
    </select>
    
    <select id="dropdown2" name="dropdown2">
        <option value="price1">Price 1</option>
        <option value="price2">Price 2</option>
    </select>
</form>

// AJAX script to handle dependent dropdowns
<script>
    $('#dropdown1').change(function(){
        var selectedOption = $(this).val();
        
        $.ajax({
            url: 'getPrices.php',
            type: 'POST',
            data: {selectedOption: selectedOption},
            success: function(response){
                $('#dropdown2').html(response);
            }
        });
    });
</script>

// getPrices.php file to handle AJAX request
<?php
// Get selected option from AJAX request
$selectedOption = $_POST['selectedOption'];

// Generate prices based on selected option
if($selectedOption == 'option1'){
    echo '<option value="price1">Price 1</option>';
    echo '<option value="price2">Price 2</option>';
} else if($selectedOption == 'option2'){
    echo '<option value="price3">Price 3</option>';
    echo '<option value="price4">Price 4</option>';
}
?>