简体   繁体   中英

Using AJAX with a contact form to remove page reload

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM