Advertisement
Guest User

MYFORM

a guest
Oct 4th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 7.50 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Simple Ajax Contact Form</title>
  6. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  7.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  8.   <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  9. <script type="text/javascript" src="js/script.js"></script>
  10. <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  11. <script type="text/javascript" src="js/jquery.validate.js"></script>
  12. <script type="text/javascript" src="js/additional-methods.js"></script>
  13. </head>
  14. <body>
  15. <button type="button" class="btn btn-danger btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
  16.  
  17. <!-- Modal -->
  18. <div id="myModal" class="modal fade" role="dialog">
  19.   <div class="modal-dialog">
  20.  
  21.     <!-- Modal content-->
  22.     <div class="modal-content">
  23.       <div class="modal-header">
  24.         <button type="button" class="close" data-dismiss="modal">&times;</button>
  25.         <h4 class="modal-title">Modal Header</h4>
  26.       </div>
  27.       <div class="modal-body">
  28.       <div class="form-style" id="contact_form">
  29.     <div class="form-style-heading">Please Contact Us</div>
  30.     <div id="contact_results"></div>
  31.     <div class="form-group">
  32.          <label><span>Name <span class="required">*</span></span>
  33.             <input type="text" name="name" id="name" required="true" class="form-control"/>
  34.         </label>
  35.  
  36.     </div>
  37.        <div class="form-group">
  38.               <label><span>Email <span class="required">*</span></span>
  39.             <input type="email" name="email" required="true" class="form-control"/>
  40.         </label>
  41.        </div>
  42.      <div class="form-group">
  43.           <label><span>Phone <span class="required">*</span></span>
  44.             <input type="text" name="phone" maxlength="15"  required="true" class="form-control" />
  45.         </label>  
  46.  
  47.      </div>
  48.      <div class="form-group">
  49.         <label><span>Attachment</span>
  50.             <input type="file" accept="doc,pdf,docx" required="true" name="file_attach" class="form-control" />
  51.         </label>
  52.       </div>
  53.       <div class="modal-footer">
  54.  
  55.    <label>
  56.             <span>&nbsp;</span><input type="submit" id="submit_btn" class="btn btn-primary" value="Submit" />
  57.  
  58.         </label>
  59.  
  60.       </div>
  61.       </div>
  62. </div>
  63.     </div>
  64.  
  65.   </div>
  66. </div>
  67.  
  68. </body>
  69. </html>
  70.  
  71.  
  72.  
  73. <?php
  74. if($_POST)
  75. {
  76.    $to_email       = "m.balajivaishnav@gmail.com"; //Recipient email, Replace with own email here
  77.     // $from_email  = "noreply@YOUR-DOMAIN.com"; //From email address (eg: no-reply@YOUR-DOMAIN.com)
  78.    
  79.    //check if its an ajax request, exit if not
  80.    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  81.        $output = json_encode(array( //create JSON data
  82.            'type'=>'error',
  83.             'text' => 'Sorry Request must be Ajax POST'
  84.         ));
  85.         die($output); //exit script outputting json data
  86.     }
  87.    
  88.     //Sanitize input data using PHP filter_var().
  89.     $user_name      = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
  90.     $user_email     = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
  91.     $phone_number   = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT);
  92.        if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
  93.        $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
  94.         die($output);
  95.     }
  96.     if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
  97.         $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
  98.         die($output);
  99.     }
  100.     if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
  101.         $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
  102.         die($output);
  103.     }
  104.    
  105.     //email body
  106.     $message_body = $message."\n\n".$user_name."\nEmail : ".$user_email."\nPhone Number :". $phone_number ;
  107.  
  108.     ### Attachment Preparation ###
  109.     $file_attached = false;
  110.     if(isset($_FILES['file_attach'])) //check uploaded file
  111.     {
  112.         //get file details we need
  113.         $file_tmp_name    = $_FILES['file_attach']['tmp_name'];
  114.         $file_name        = $_FILES['file_attach']['name'];
  115.         $file_size        = $_FILES['file_attach']['size'];
  116.         $file_type        = $_FILES['file_attach']['type'];
  117.         $file_error       = $_FILES['file_attach']['error'];
  118.  
  119.         //exit script and output error if we encounter any
  120.         if($file_error>0)
  121.         {
  122.             $mymsg = array(
  123.             1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
  124.             2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
  125.             3=>"The uploaded file was only partially uploaded",
  126.             4=>"No file was uploaded",
  127.             6=>"Missing a temporary folder" );
  128.            
  129.             $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
  130.             die($output);
  131.         }
  132.        
  133.         //read from the uploaded file & base64_encode content for the mail
  134.         $handle = fopen($file_tmp_name, "r");
  135.         $content = fread($handle, $file_size);
  136.         fclose($handle);
  137.         $encoded_content = chunk_split(base64_encode($content));
  138.         //now we know we have the file for attachment, set $file_attached to true
  139.  
  140.      $allowedExts = array("pdf", "doc", "docx");
  141.     $extension = end(explode(".", $_FILES["file_attach"]["name"]));  
  142. if ((($_FILES["file_attach"]["type"] == "application/pdf") || ($_FILES["file_attach"]["type"] == "application/msword")) && ($_FILES["file_attach"]["size"] < 20000000) && in_array($extension, $allowedExts))
  143.    {
  144.      if ($_FILES["file"]["error"] > 0)
  145.      {
  146.         echo "Return Code: " . $_FILES["file_attach"]["error"] . "<br>";
  147.       }
  148.       else
  149.       {
  150.  
  151. $file_attached = true; 
  152.       }
  153.   }
  154.        
  155.     }
  156.  
  157.     if($file_attached) //continue if we have the file
  158.     {
  159.         $boundary = md5("sanwebe");
  160.        
  161.         //header
  162.         $headers = "MIME-Version: 1.0\r\n";
  163.         $headers .= "From:".$user_email."\r\n";
  164.         $headers .= "Reply-To: ".$user_email."" . "\r\n";
  165.         $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
  166.  
  167.         //plain text
  168.         $body = "--$boundary\r\n";
  169.         $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
  170.         $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
  171.         $body .= chunk_split(base64_encode($message_body));
  172.    
  173.         //attachment
  174.         $body .= "--$boundary\r\n";
  175.         $body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
  176.         $body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
  177.         $body .="Content-Transfer-Encoding: base64\r\n";
  178.         $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
  179.         $body .= $encoded_content;
  180.  
  181.     }else{
  182.         //proceed with PHP email.
  183.         $headers = "From:".$user_email."\r\n".
  184.         'Reply-To: '.$user_email.'' . "\n" .
  185.         'X-Mailer: PHP/' . phpversion();
  186.         $body = $message_body;
  187.     }
  188.  
  189.     $send_mail = mail($to_email, $subject, $body, $headers);
  190.    
  191.  
  192.     if(!$send_mail)
  193.     {
  194.         //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
  195.         $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
  196.         die($output);
  197.     }else{
  198.         $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email'));
  199.         die($output);
  200.     }
  201.  
  202. }
  203.  
  204. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement