-1

The contact form I'm using redirects after message has been sent but i would like the user to get a 'Message Sent' alert that would appear & disappears somewhere on the contact form instead. Meaning the page never refreshes nor does the user get taken away from the page or contact form.

Is this possible & if so could someone please show me the code i need to add to the current below...

<form id="contact-form-face" class="clearfix"      action="http://www.demo.com/php/contactengine.php">
    <input type="text" name="email" value="Email"     onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
    <textarea name="message" onFocus="if (this.value     == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
    <input class="contact_btn" name="submit"     type="submit" value="Send Message" />
</form>

And the PHP:

<?php

$EmailFrom = "myemail";
$EmailTo = "myemail";
$Subject = "";
$Email = Trim(stripslashes($_POST['email'])); 
$Message = Trim(stripslashes($_POST['message'])); 

// validation
$validationOK=true;
if (!$validationOK) { 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
}

// prepare email body text
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
   print "<meta http-equiv=\"refresh\"   content=\"0;URL=contactthanks.php\">";
}
else{
   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Any help would be greatly appreciated as I'm very new to this side of things.

MKDesigns
  • 55
  • 7
  • You can submit the form via `AJAX`, google it ;) – Axel Guilmin Jul 06 '15 at 07:54
  • Unfortunately the reason I'm an here is to gain expertise from someone who can talk me through it. To tell me to Google it defeats the purpose of this site but thanks anyway for pointing out the obvious – MKDesigns Jul 06 '15 at 07:57
  • IMO it's good to go through some tutorials if you never heard about AJAX before. Just a code snippet is not enough to get the concept behind AJAX. – Axel Guilmin Jul 06 '15 at 08:05

1 Answers1

3

You can simply use AJAX to do this:

$(function () {
  $("#contact-form-face").submit(function (e) {
    e.preventDefault();
    $.post($(this).attr("action"), $(this).serialize(), function () {
      alert("Submitted!");
    });
  });
});

If the URL is on another domain, then CORS will be blocked. In order to avoid that, use:

<?php
  header("Access-Control-Allow-Origin: *");

On the PHP file, that is being called.


For the form interaction:

$(function () {
  $("#contact-form-face").submit(function (e) {
    e.preventDefault();
    $.post($(this).attr("action"), $(this).serialize(), function () {
      $("#post-ajax").html("Your Message Has Been Sent! Thank You For Contacting Us.").show();
      $("#contact-form-face").hide();
    });
  });
});
<!-- CONTACT FORM -->
<div class="span9 contact_form">
  <div id="note"></div>
  <div id="fields">
    <div id="post-ajax" style="display: none;"></div>
    <form id="contact-form-face" class="clearfix" action="http://www.demo.com/php/contactengine.php">
      <input type="text" name="email" value="Email" onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
      <textarea name="message" onFocus="if (this.value     == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
      <input class="contact_btn" name="submit"     type="submit" value="Send Message" />
    </form>
  </div>
</div><!-- //CONTACT FORM -->
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252