How to output "Thanks" text, instead of opening a separate URL using formmail.php

224 Views Asked by At

This is my site that I'm working on the contact page.

I'm using formmail.php for the contact form (http://www.tectite.com/)

When the user submits the form I want it to say something like "Thank you, your message has been sent." But I want this text to be displayed on the page where the form is - NOT open a separate URL.

There is already a way to redirect to a separate page by using:

    <input type="hidden" name="good_url" value="http://yoursite.com/thanks.htm" />

However since my site is a single page scrolling site, I want to avoid navigating away.

3

There are 3 best solutions below

4
On

You can simply use ajax functionality to do it

$.ajax({
                url: "page which you want to call on action",
                data: $( "form" ).serialize(),
                type: 'POST',
                async: false,
                success: function(responseObject) {
                   // $("#hiddendiv").show(); // OR
                   // $("#hiddendiv").html("thanks message");
                }
            });
1
On

You can use Ajax as @Abhishek answered. Or do something like below.

if(isset($_POST['submit']))
{
   //Your code for mailing
   header('location:formmail.php?message=set');
}

Then at the place you want to show the message

if(isset($message))
{
   echo "Thanks";
}
2
On

Try this ajax

  $.ajax({
          type: "POST",
          url: "send.php",
          data: $(form).serialize(),
          timeout: 3000,
          success: function() {
            $("#contactForm").hide();
            $('#divContactFrm').html("<div id='divSuccessMsg'></div>");
            $('#divSuccessMsg').html("Thank you!")
            .hide()
            .fadeIn(1500, function() { $('#divSuccessMsg'); 
        });
          }