How to get the sucess message in the same page after submitting the contact form?

How to get the sucess message in the same page after submitting the contact form?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Redirect Trap: Keeping Users on Your Contact Form Page

The behavior you are experiencing—where the page reloads and displays your form fields again instead of showing a success message—is a classic symptom of how HTML forms handle submissions by default. When a user clicks "Submit" on a standard <form> tag, the browser sends a POST request to the server, processes the data in your PHP script, and then automatically redirects back to the URL where the form resides (in this case, form.php).

This happens because your form's action attribute points to itself. The browser assumes that once the data has been sent and processed, the user should be returned to the starting point. To fix this and display a success message on the same page without a redirect, you need to intercept the response before the browser performs its automatic navigation.

Overriding the Default Redirect

The most straightforward solution is to tell the server explicitly not to send a header that triggers a redirect. In PHP, the default behavior after processing a form is to output content and close the script, which often results in the browser refreshing the page or reloading the form data if no explicit output is sent.

You can prevent this by adding a header() directive at the very top of your form.php file. This header tells the browser to stop automatic redirection and treat the response as a standard HTML page containing whatever content you print.

<?php
// Prevent automatic redirect behavior
header('Content-Type: text/html; charset=utf-8'); 

$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$message = $_POST['message'] ?? '';
$from = 'From: agriindiaexp.com'; 
$to = 'shridhar.kagi@gmail.com'; 
$subject = 'Email Inquiry';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

// Check if the form was actually submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (mail($to, $subject, $body, $from)) { 
        $success = "Message successfully sent.";
    } else {
        $success = "Message sending failed, please try again.";
    }
}

// Output the success message or form again
echo "<h2>" . ($success ?? "Contact Form") . "</h2>";

// Display the form again so users can send another message
?>
<form method="post" action="">
    <input name="name" required="required" placeholder="Your Name">
    <input name="email" type="email" required="required" placeholder="Your Email">
    
    <div class="clearfix"></div>
    <textarea name="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
    <div class="submit">
        <input id="submit" name="submit" type="submit" value="Submit">
    </div>
</form>

Handling Form Data Safety

In your original code, the inputs lacked type attributes for standard text fields and relied heavily on required="required" without HTML5 validation context. While required works in modern browsers, it is good practice to explicitly set types like <input type="text">. Additionally, accessing $_POST variables directly can lead to errors if the form hasn't been submitted yet (e.g., when the page loads initially).

Using the null coalescing operator (??) or checking $_SERVER['REQUEST_METHOD'] === 'POST' ensures that your script handles both the initial page load and the post-submission state gracefully. This prevents "Undefined index" warnings from appearing in your error logs.

The Trade-Off: Security Considerations

While displaying a success message on the same page improves user experience, it introduces a specific security pattern known as Open Redirect vulnerabilities if not handled carefully. By keeping the user on the current page, you are essentially telling them where to go next. If you were to redirect users elsewhere (e.g., header("Location: /thanks")), you must ensure that destination URL is hardcoded and cannot be manipulated by user input.

Since your goal here is to stay on the same page, this specific risk is mitigated because there is no redirection happening at all. However, if you decide to move away from this approach in the future using a framework like Laravel, you should use the redirect()->now()->route() method instead of manual header functions to ensure strict validation against open redirect attacks.

For most simple PHP scripts, the header('Content-Type: ...') approach is sufficient to override the default browser behavior and display your custom success message immediately after submission.

Our network