javascript – PHP isset Form Submit and jQuery on Submit Function conflict-ThrowExceptions

Exception or error:

I need to do this on one page as i am using phpmailer file upload,

The form value are added to the google sheets but the phpmailer stopped working, when i remove this script, then the mailer works.

$(function() {
  $('#frm_details').on('submit', function(e) {
    e.preventDefault();
    $(this).fadeOut('slow');
    $('.warning-box').fadeOut('slow');
    $('.main-form #warranty-claim').fadeOut('slow');
    $('.loader').fadeIn('slow');
    $.ajax({
      url: "https://script.google.com/macros/s/AKfycbasSzNxkBEdGlu_BdnasdaYNVYasdasZxRvmFgxgSjh8szsQtqC/exec",
      type: "GET",
      data: $(this).serialize(),
      success: function(data) {
        $('#success_message').fadeIn('slow');
        $('.loader').fadeOut('slow');
      },
      error: function(data) {
        console.log('there is an error');
      }
    });

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="frm_details" method="post" enctype="multipart/form-data">
  input type="hidden" name="MAX_FILE_SIZE" value="10000000">
  <input name="userfile[]" type="file" multiple="multiple" id="imageinput" class="form-control-file">
  <input type="submit" name="submit" value="Claim" class="w-100 h-100 form-control text-uppercase text-center d-block mx-auto">
</form>

Here’s my mailer

    use PHPMailer\PHPMailer\PHPMailer;
    if (isset($_POST['submit'])) {
if (array_key_exists('userfile', $_FILES)) {
            require 'vendor/autoload.php';
            // Create a message
            $mail   = new PHPMailer;
            $mail->setFrom('sample@example.com', 'John Doe');
            $mail->addAddress('me@personal.com', 'Me Myself');
            $mail->Subject = $ordernumber . ' - Warranty';
            $mail->IsHTML(true);
            $mail->Body .= '<p style="letter-spacing: 1px;margin-bottom: 10px;">Hi,</p>';
            $mail->Body .= '<p style="letter-spacing: 1px;margin-bottom: 10px;">A warranty claim has been submitted. Please look into this.</p>';

            //Attach multiple files one by one
            for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
                $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
                $filename   = $_FILES['userfile']['name'][$ct];
                if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
                    $mail->addAttachment($uploadfile, $filename);
                }
                else {
                    $msg .= 'Failed to move file to ' . $uploadfile;
                }
            }

            if (!$mail->send()) {
                $msg .= "Mailer Error: " . $mail->ErrorInfo;
            }
            else {
                $msg .= "Message sent!";
            }

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *