简体   繁体   English

表单提交后,如何防止页面刷新并使它保留在同一选项卡部分中

[英]After form submit how to prevent page refresh and make it to remain in same tab section

I am using a bootstrap template which has all page code in index page and redirecting the tab for all the pages by using id with in index page.In contact form tab section i have created a form to perform file upload and after i have submit my form it redirects to home page tab.how to make it to remain in same page.I have written the php action too in the same page but its not supporting. 我正在使用一个引导模板,该模板具有索引页面中的所有页面代码,并通过在索引页面中使用id重定向所有页面的选项卡。在联系表单选项卡部分中,我创建了一个表单来执行文件上传,并在提交我的表单后形式它重定向到主页tab.how如何使其保留在同一页面中。我也在同一页面中编写了php操作,但它不支持。

Until i have refer we cant able to use ajax in fileupload hence we need to use this plugin. 在我引用之前,我们无法在fileupload中使用ajax,因此我们需要使用此插件。 jquery form plugin but i have no idea on how to implement this plugin in my form. jQuery表单插件,但我不知道如何在表单中实现此插件。

<form id="attach" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<div>
  <p>
    <label for="tele">Upload Your Resume:</label><br>
    <input id="tele" name="filename" type="file" />
  </p> 
</div>
<input class="formbtn" type="submit" value="Send Message" />
</form>

php PHP

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

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

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

   // Get the sender's name and email address plug them a variable to be used later
   $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">";

    // 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>';
        }


    // Get all the values from input
    $name = $_POST['name'];
    $email_address = $_POST['email'];
    $message = $_POST['message'];

    // Check the email address
    if (!eregi( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
    {
        $errors .= "\n Error: Invalid email address";
    }

   // 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 = "\n\n Name: $name \n\n Email: $email_address \n\nMessage: \n\n $message \n\nHere is your 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))
      {
         ?>
         <div><center><h1>Mail Sent successfully !!</h1></center></div>
         <?php
      }else
      {
         ?>
         <div><center>
           <h1>Error !! Unable to send Mail..</h1></center></div>
         <?php
      }
   }
}
?>

不要使用任何操作路径,这样它将调用同一页面(根据上述当前脚本):

<form action="" method="post" enctype="multipart/form-data">

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

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