2

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.

Willi Mentzel
  • 21,499
  • 16
  • 88
  • 101
user2419684
  • 33
  • 1
  • 3

3 Answers3

3

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");
                }
            });
Abhishek Sanghvi
  • 4,412
  • 6
  • 25
  • 34
  • `$.post()` is the shorthand. Also, it's suggested to show a "Loading" message on-screen, or users won't know what's going on when AJAX is being executed behind the scene. – Raptor Aug 27 '14 at 09:49
  • I just gave an Idea of how we can go about it. Also i think @user2419684 is a programmer and he can manage how UI would appear.. check http://stackoverflow.com/a/12820123/165370 – Abhishek Sanghvi Aug 27 '14 at 09:50
  • I've tried both this and @Kanantuteja solution below, but it is still displaying the default formmail page - how can I override this? – user2419684 Aug 27 '14 at 10:11
  • You need to provide more details (form mail page or send page ) in order to help you better – Abhishek Sanghvi Aug 27 '14 at 10:14
0

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'); 
        });
          }
Kanan tuteja
  • 135
  • 10
-2

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";
}
arunrc
  • 632
  • 2
  • 12
  • 26
  • 2
    Huh? If the page doesn't reload, the PHP `isset()` won't check again. Also, don't forget to put an `exit()` after `header('Location: url');`. Wrong header syntax too. – Raptor Aug 27 '14 at 09:48