Using AJAX with a contact form to remove page reload

Kevlar

I have seen lots of questions about this on here and have taken the code I have now from several answers.

But for some reason I cant get it to work and I cant figure out why.

I have a HTML form with the code here:

<form id="quick_contact" name="quick_contact" action="" method="POST">
    <div class="input-control text">
        <input id="name" name="name" type="text" value="" placeholder="Enter name here">
    </div>
    <div class="space10">&nbsp;</div>
    <div class="input-control email">
        <input id="email" name="email" type="email" value="" placeholder="Enter email address here"/>
    </div>
    <div class="space10">&nbsp;</div>
    <div class="input-control textarea">
        <textarea id="comments" name="comments" placeholder="Enter Comments Here"></textarea>
    </div>
    <div class="space10">&nbsp;</div>
    <button id="quick_submit" name="quick_submit" onclick="quickContact()">Send</button>                      
</form>

And I have my jquery here UPDATED as of Thomas' answer:

<script type="text/javascript">
    function quickContact(){
        $.ajax({
        type: "POST",
        url: "quick-contact.php",
        data:
             {
                 name: $('#name').val().trim(),
                 email: $('#email').val().trim(),
                 comments: $('#comments').val().trim(),                 
             },            
        success: function(html) {
                     var submitted = $.trim(html);
                     if (submitted) {
                         alert("Thanks for your submission");
                         $('#quick_contact')[0].reset();
                         return;
                     } else {
                         alert("Failed to submit.");
                         return false;
                }
        }
    });         
};
</script>

And here is the PHP which handles the email side of things in a file called "quick-contact.php again updated as of Thomas' answer:

if(isset($_POST) == true){
    $status = 1 // init to one, assume there will not be an error
    //Store the entered values in the variables
    $name = mysql_escape_string(trim($_POST['name']));
    $email = mysql_escape_string(trim($_POST['email']));
    $comments = mysql_escape_string(trim($_POST['comments']));
    $comments = str_replace('\r\n','<br>',$comments);

    // EMAIL HEADERS
    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=utf-8\n";
    $headers .= "X-Priority: 3\n";
    $headers .= "X-MSMail-Priority: Normal\n";
    $headers .= "X-Mailer: php\n";          
    $headers .= "From: *****<*****@l*****>\n";

    //SEND EMAIL TO BRANCH
    // EMAIL TITLE
    $subject = $name . " " . get_content(3344);

    //message
    $message1 = "<style type=\"text/css\">";
    $message1 .= "div { font-family: Arial, Verdana, Tahoma; font-size: 10pt; line-height: 120%; }";
    $message1 .= "h1 { margin: 0; font-size: 14pt; }";
    $message1 .= "h2 { margin: 0; font-size: 12pt; }";
    $message1 .= "span { font-size: 9pt; font-weight: bold; }";
    $message1 .= "</style>\n";
    $message1 .= "<div>";
    $message1 .= "<p>" . $name . " " . get_content(3344) . "</p>\n";
    $message1 .= "<p>" . get_content(3345) . "</p>\n";
    $message1 .= "<p><b>" . ucwords(get_content(2869)) . ":</b> " . $name . "<br />";
    $message1 .= "<b>" . ucwords(get_content(27)) . ":</b> " . $email . "<br />";
    $message1 .= "<b>" . ucwords(get_content(1258)) . ":</b> " . $comments . "<br />";
    $message1 .= "</p>\n";
    $message1 .= get_content(893); // King Regards, 
    $message1 .= "<br /><br />";
    $message1 .= "<img src=\"***********\" alt=\"*******\">";
    $message1 .= "<br />";
    $message1 .= "</div>";

    //SEND CUSTOMER AN EMAIL
    // EMAIL TITLE
    $subject2 = get_content(392);

    //message
    $message2 = "<style type=\"text/css\">";
    $message2 .= "div { font-family: Arial, Verdana, Tahoma; font-size: 10pt; line-height: 120%; }";
    $message2 .= "h1 { margin: 0; font-size: 14pt; }";
    $message2 .= "h2 { margin: 0; font-size: 12pt; }";
    $message2 .= "span { font-size: 9pt; font-weight: bold; }";
    $message2 .= "</style>\n";
    $message2 .= "<div>";
    $message2 .= "<p>" . $name . ",</p>\n";
    $message2 .= "<p>" . get_content(392) . "</p>\n";
    $message2 .= "<p>" . str_replace("{TEL_NUMBER}", $header_branch_details[0]['Tel'], str_replace("{BRANCH_EMAIL}", $header_branch_details[0]['SalesEmail'], get_content(2764))) . "</p>\n";
    $message2 .= get_content(893); // King Regards, 
    $message2 .= "<br /><br />";
    $message2 .= "<img src=\"*********\" alt=\"*********\">";
    $message2 .= "<br />";
    $message2 .= "</div>";

    //Send branch email
    $success = mail('***@****.com', $subject, $message1, $headers); 
    //Send customer email
    $success2 = mail($email, $subject2, $message2, $headers);

    if (!$success) {
       $status = 0;
    }   
    echo $status;
}

Sorry about the mass of code I really hope someone can help me here

Thomas Powers

Change the submit type to a button type with an onClick event that calls doSomething().

Then, have doSomething() run the ajax call, like so:

function doSomething() {
    $.ajax({
         type: 'post',
         url: 'quick-contact.php',
         data:
         {
             name: $('#name').val().trim(),
             email: $('#email').val().trim(),
             comments: $('#comments').val().trim(),                 
         },
         success: function(html) {
             var status = $.trim(html);
             if (status) {
                 alert("Thanks for your submission");
                 $('#quick_contact')[0].reset();
                 return;
             } else {
                 alert("Failed to submit.");
                 return false;
             }
         }
    });
 }

Then in your PHP, all you have to do is check if $_POST isset.

 if (($_POST)) {
     $status = 1; // init to one, assume there will not be an error
     $name = $_POST['name'];
     //etc, but FILTER the data coming from the client
 }

Also, you may want to do something like this near your mail() function:

$success = mail(//etc);
if (!$success) {
   $status = 0;
}

echo $status;

If you still have questions, let me know. I'm happy to help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Submit Contact Form w/o Reloading Page Using Ajax

From Dev

Reload a page after successfully submit a form using ajax

From Dev

Reload a page after successfully submit a form using ajax

From Dev

Don't reload page after form submission using ajax and jquery

From Dev

Echo in the same page after using contact form

From Dev

how to reload a page using ajax success function

From Dev

jQuery AJAX contact form with PHP mailer jumping to mail page

From Dev

PHP / AJAX Contact Form - Page Opens Mid-Way

From Dev

Magento Contact form in ajax

From Dev

Contact form not displaying on the page

From Dev

Prevent page reload after form submit / node (no ajax available)

From Dev

Prevent page reload and redirect on form submit ajax/jquery

From Dev

How to stop contact form 7 reload the page after submit without include wp_head in header file?

From Dev

Form reset on page reload?

From Dev

AJAX Page section reload

From Dev

Sending contact form data using Ajax/jQuery/PHP

From Dev

Send email from contact form in Laravel 5 using jQuery AJAX

From Dev

How to reload a specific form after submit success using Ajax?

From Dev

Submit form without reload using jQuery AJAX in PHP MySQL

From Dev

How to return specific table from page using Ajax reload

From Dev

reload a div without reloading the whole page using ajax/jquery

From Dev

How to return specific table from page using Ajax reload

From Dev

Save data using JQuery Ajax to avoid page reload

From Dev

How to post and get comment using laravel ajax without reload page?

From Dev

I want to reload a datatable using ajax without refreshing the whole page

From Dev

Contact Form 7 AJAX Callback

From Dev

Ajax Contact Form Loader issue

From Dev

Contact form with AJAX not working in Wordpress

From Dev

Django Wagtail ajax contact form

Related Related

  1. 1

    Submit Contact Form w/o Reloading Page Using Ajax

  2. 2

    Reload a page after successfully submit a form using ajax

  3. 3

    Reload a page after successfully submit a form using ajax

  4. 4

    Don't reload page after form submission using ajax and jquery

  5. 5

    Echo in the same page after using contact form

  6. 6

    how to reload a page using ajax success function

  7. 7

    jQuery AJAX contact form with PHP mailer jumping to mail page

  8. 8

    PHP / AJAX Contact Form - Page Opens Mid-Way

  9. 9

    Magento Contact form in ajax

  10. 10

    Contact form not displaying on the page

  11. 11

    Prevent page reload after form submit / node (no ajax available)

  12. 12

    Prevent page reload and redirect on form submit ajax/jquery

  13. 13

    How to stop contact form 7 reload the page after submit without include wp_head in header file?

  14. 14

    Form reset on page reload?

  15. 15

    AJAX Page section reload

  16. 16

    Sending contact form data using Ajax/jQuery/PHP

  17. 17

    Send email from contact form in Laravel 5 using jQuery AJAX

  18. 18

    How to reload a specific form after submit success using Ajax?

  19. 19

    Submit form without reload using jQuery AJAX in PHP MySQL

  20. 20

    How to return specific table from page using Ajax reload

  21. 21

    reload a div without reloading the whole page using ajax/jquery

  22. 22

    How to return specific table from page using Ajax reload

  23. 23

    Save data using JQuery Ajax to avoid page reload

  24. 24

    How to post and get comment using laravel ajax without reload page?

  25. 25

    I want to reload a datatable using ajax without refreshing the whole page

  26. 26

    Contact Form 7 AJAX Callback

  27. 27

    Ajax Contact Form Loader issue

  28. 28

    Contact form with AJAX not working in Wordpress

  29. 29

    Django Wagtail ajax contact form

HotTag

Archive