PHP Contact Form on the same page

I have a contact form below and when I submit the contact form it sends me in another page saying Your Message was sent ! but I don’t want that so I need to just display a simple message inside the contact form saying Your Message was sent and I’m very beginner with PHP and other backend languages so if someone would help me this would appreciate a lot.

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent= " From:  $name, \n Email: $email \n Message: $message";
$recipient = "";
$subject = "New Email";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
echo "Your Message was sent !";
<form method="POST" action="contact-form.php">      
         <input name="name" type="text" class="message" placeholder="Name" />  
     <input name="email" type="text" class="message" placeholder="Email"  />
  <textarea name="message" class="message" placeholder="Your Message"></textarea>
             <input type="submit" value="SUBMIT"/>

It has been a very long time since I looked at php, and I never knew it well, but I think that what you want to do is remove contact-form.php from the form action and instead have your php script watch for the POST submission.

If you leave the action blank, it will post (submit the form) to the same page.

The first thing you need to be doing, is checking to see whether or not the form was submitted, before getting values, trying to send an email, and posting the response message. Easiest way, is to check to see if the submit button was submitted:

if(isset($_POST["submit"])) {
   // process form data, send email, output message

You should also ensure that you are scrubbing the data coming from a form to ensure there is no malicious code in it. (Look into SQL injection and XSS Scripting)

PHP won’t automagically re-populate the HTML form, so that’s something you have to do yourself:

<input value="<?=$email?>" ..... />

I believe the value attribute should override the placeholder one. And depending on how you have things set up, you might have to check whether or not a value exists for $email before using it:

<input value="<?php if(isset($email)) print $email ?>" ..... />

You could also try processing the data through an Ajax call. Have a nicely formatted message that says the message was sent while staying on the current page.