What potential security risks should be considered when dynamically changing the content of an iframe?

When dynamically changing the content of an iframe, potential security risks to consider include cross-site scripting (XSS) attacks and content injection. To mitigate these risks, it is important to properly sanitize and validate the content being loaded into the iframe to prevent malicious scripts or content from being executed within the iframe.

<?php
// Sanitize and validate the URL before setting it as the src attribute of the iframe
$iframeSrc = filter_var($_POST['iframe_src'], FILTER_VALIDATE_URL);

echo '<iframe src="' . $iframeSrc . '"></iframe>';
?>