douchun1859 2014-08-21 13:49
浏览 39
已采纳

使用带有联系表单的AJAX来删除页面重新加载

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('
','<br>',$comments);

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

    //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>
";
    $message1 .= "<div>";
    $message1 .= "<p>" . $name . " " . get_content(3344) . "</p>
";
    $message1 .= "<p>" . get_content(3345) . "</p>
";
    $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>
";
    $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>
";
    $message2 .= "<div>";
    $message2 .= "<p>" . $name . ",</p>
";
    $message2 .= "<p>" . get_content(392) . "</p>
";
    $message2 .= "<p>" . str_replace("{TEL_NUMBER}", $header_branch_details[0]['Tel'], str_replace("{BRANCH_EMAIL}", $header_branch_details[0]['SalesEmail'], get_content(2764))) . "</p>
";
    $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

  • 写回答

1条回答 默认 最新

  • douyudouchao6779 2014-08-21 14:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘