How can PHP be used to prevent redirection to a different page when attempting to create a reply in a forum thread?

When creating a reply in a forum thread, PHP can be used to prevent redirection to a different page by using AJAX to submit the form asynchronously. This way, the page does not need to reload, allowing the user to stay on the same page after submitting the reply.

<?php
// PHP code to handle forum reply submission without redirection

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["reply_content"])) {
    // Process the reply content here

    // Return a response to the AJAX request
    echo json_encode(array("success" => true, "message" => "Reply submitted successfully"));
    exit;
}
?>