How can PHP be used to automatically insert related values into input fields?

When working with forms in PHP, you may want to automatically insert related values into input fields based on certain conditions or data retrieved from a database. This can be achieved by using PHP to dynamically generate the HTML form elements and pre-fill them with the desired values.

<?php
// Assume $relatedValue is the value you want to insert into the input field

// Check if the related value is available
if(isset($relatedValue)){
    // Generate the HTML input field with the related value pre-filled
    echo '<input type="text" name="related_field" value="' . $relatedValue . '">';
} else {
    // Generate a regular input field if the related value is not available
    echo '<input type="text" name="related_field">';
}
?>