简体   繁体   English

如何使用malsup表单jquery成功发送邮件后在表单提交中显示成功消息而无需刷新页面

[英]How to display success message in a form submit after sending a mail successfully using malsup form jquery without page refresh

I need to display success message after uploading a form and send it as an attachment in mail.I need to implement this by using malups form js .How to write jquery function for this and also i have to do this without refresh the page. 我需要在上传表单后显示成功消息并将其作为附件发送到邮件中。我需要使用malups form js来实现此目的。 如何为此编写jquery函数,并且我必须这样做而不刷新页面。

By using the ajaxForm function() i can able to display alert instead of this i need to show a success message in a div after sending the mail successfully with attachment. 通过使用ajaxForm function(),我可以显示警报而不是显示警报,我需要在成功发送带有附件的邮件后在div中显示成功消息。

php 的PHP

 <?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

   // Set the "To" email address
   $to="abc@gmail.com";

    //Subject of the mail
   $subject="Join Us E-mail with Resume attachment";



    // Check for empty fields

    if($_FILES['filename']['tmp_name']==""){
            echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Please upload your resume</font>';
        }

   //if(empty($_POST['name'])  || empty($_POST['email']) || empty($_POST['message']))
//  {
//      /*$errors .= "\n Error: all fields are required";*/
//       echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Please upload resume</font>';
//  }



   // Now Generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // Now Store the file information to a variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $file_name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];

   // Now here we setting up the message of the mail
   $message = "Uploaded file: $file_name";

   // Check if the upload succeded, the file will exist
   if (file_exists($tmp_name)){

      // Check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){

         // Now Open the file for a binary read
         $file = fopen($tmp_name,'rb');

         // Now read the file content into a variable
         $data = fread($file,filesize($tmp_name));

         // close the file
         fclose($file);

         // Now we need to encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));
     }

      // Now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // Next, we'll build the message body note that we insert two dashes in front of the  MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

      // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$file_name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // Thats all.. Now we need to send this mail... :)
      if (@mail($to, $subject, $message, $headers))
      {
       echo "success"; 
      }else
      {
         echo "failed";
      }
   }
}
?>

HTML 的HTML

<form id="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

<input id="tele" type="file" name="filename"/>
<br/>
<input  class="formbtn" type="submit" id="upload" value="Upload" />
</form>

Jquery jQuery的

<script>
$(document).ready(function () {

    $('#myform').validate({
        rules: {
            filename: {
                required: true,
                extension: "docx|rtf|doc|pdf"
            }
        },
        messages: {
            filename: {
                required: "Please upload resume",
                extension: "Please upload valid file formats"
            }
        }
    });

});
</script>
<script> 
    $(document).ready(function() { 
        $('#myform').ajaxForm(function() { 

           alert('success');
        }); 
    }); 

</script> 

Before the below code make sure you have a div of id msg-div (name's your choice) and by default set its display to none 在下面的代码之前,请确保您有一个ID为msg-div (由您选择名称),并且默认情况下将其显示设置为none

<script> 
    $(document).ready(function() { 
        $('#myform').ajaxForm(function() { 
           $('#msg-div').css('display','block')
           $('#msg-div').html('success message here')
        }); 
    }); 
</script> 

If I understand well your problem, you now are able to alert the message, but you want to load it into a div. 如果我很了解您的问题,那么您现在就可以发出警报,但是您希望将其加载到div中。

That should be as easy as: 那应该很简单:

$("#yourContainerDiv").html(yourAlertString);

If you need to create the div you can add any HTML you need on your string and attach it to the final container. 如果需要创建div,则可以在字符串上添加所需的任何HTML,并将其附加到最终容器中。 You can also use: 您还可以使用:

$("#containerWhereYouWantToAppendMessage").append(htmlCodeWithMessage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击提交按钮后,如何使用jQuery淡化表单并在成功时显示消息? - How to fade a form and display a message on success using jQuery after clicking the submit button? 如何显示评论表单提交而不使用AJAX刷新页面? - How to display comment form submit without refresh the page using AJAX? 提交表单模式后显示成功消息 - Display success message after submit form modal 仅在使用Jquery Ajax将邮件发送给收件人之后,如何显示成功消息 - How to display success message only after sending the mail to recipient using Jquery Ajax 用户提交表单后如何显示成功消息 - How can I display a success message after the user submit the form 使用PHP AJAX显示成功消息而不显示页面刷新后,再次显示Form div - Show Form div again after displaying success message without Page Refresh Using PHP AJAX 提交没有表单刷新的html表单,在html页面上显示成功消息 - Submit html form without page refresh show on html page a success message 提交后用成功消息替换HTML表单,表单使用单独的php文件发送邮件 - Replacing HTML form with success message after submit, form sends mail using separate php file 提交电子邮件表单而无需刷新页面NO JQUERY - Submit email form without page refresh NO JQUERY 提交表单后刷新jQuery Mobile页面 - Jquery mobile page refresh after form submit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM