php - 如何显示弹出谢谢消息而不是其他页面

I am using a form-to-email.php for a contact form in my website, but I don't really understand php code.

The ready-to-use form-to-email.php file has a line "redirect to thank you page," and I have to build a thank you page for the action.

But I hope the UX could be easier like a pop-up thank you message instead of another page.

Anyone could help me to create the lines? Thank you very much!!

Below is the complete code of form-to-email.php, the line "header('Location: thank-you.html')" is the redirect path, and I'm wondering is there any way to modify the lines?

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$agree = $_POST['agree'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}


$email_from = 'receiver@gmail.com';//<== update the email address
$email_subject = "letter from customer";
$email_body = "$name
".
    "Message:
$message
LINE ID: $line".
$to = "receiver@gmail.com";//<== update the email address
$headers = "From: $email_from 
";
$headers .= "Reply-To: $visitor_email 
";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(
+)',
              '(+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
   
?> 

</div>

If you want to use JavaScript, you can use this code to show the message "Thank you for your message" in an alert() box:

Replace header('Location: thank-you.html') with:

echo'
   <script>
   window.onload = function() {
      alert("Thank you for your message");
      location.href = "index.php";  
   }
   </script>
';

You can also use below AJAX script to handle it. It will not reload the page and it will give you good user experience. You must include jquery library to work.

 $.ajax({
   url: "ready-to-use form-to-email.php",
   type: "post",
   data: {id:xyz},
   success: function (response) {
      // you will get response from your php page (what you echo or print)   
      alert('Success') ;
   },
   error: function(jqXHR, textStatus, errorThrown) {
      console.log(textStatus, errorThrown);
   }

});