0

I think this is a very quick problem for someone with the right eye... I myself have been banging my head against the wall despite doing a lot of looking into it.

I have a form with a file attachment field which I want to send without leaving the page i.e. using jquery/ajax to do it. I know that it's definitely the file attachment that's messing things up because when I remove all references to it the functionality works perfectly, but I don't know what to do about it.

The HTML is basically a normal form so I won't bore you with that. Here's the jquery/ajax stuff:

$(form).submit(function(){
      $.post($(this).attr('action'), $(this).serialize(), function(response){
            // success stuff
      },'json');
      return false;
   });

I already know that it's the serializing of the data that's causing the problem because a file can't be serialized. Does anyone have any suggestion as to what I should do? I would like to use the $.post function as opposed to the $.ajax one if possible, purely for its simplicity. I haven't yet found a solution out there that does it with the $.post function, stays on the same page and allows a file attachment.

Just in case it's real easy to do, if anyone would be able to offer a solution that would work for multiple files (as opposed to just one) including what I would need to do to the PHP file I'd be really, really grateful but I wouldn't want to take up too much of anyone's time! Here's the PHP just in case anyone would be able to help with this (the headers bit is near the bottom). FWIW the php file works fine at present i.e. standard submit on the form then passing it to the php file:

<?php
    error_reporting(E_ALL);
    //Only process POST requests
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace
    $name = strip_tags(trim($_POST["name"])); // Remove tags from field
    $name = str_replace(array("\r","\n"), array(" "," "),$name); // Replace carriage returns and newlines with spaces
    $name = filter_var($name, FILTER_SANITIZE_STRING);
    $email = $_POST["email"];
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitize email address
    $phonenumber = filter_var($_POST["phonenumber"],FILTER_SANITIZE_NUMBER_INT);
    $fireservice = $_POST["fireservice"];
    $customername = filter_var($_POST["customername"],FILTER_SANITIZE_STRING);
    $customeraddress1 = $_POST["customeraddress1"];
    $customeraddress2 = $_POST["customeraddress2"];
    $customeraddress3 = $_POST["customeraddress3"];
    $customeraddress4 = $_POST["customeraddress4"];
    $customerphonenumber = filter_var($_POST["customerphonenumber"],FILTER_SANITIZE_NUMBER_INT);
    $carername = $_POST["carername"];
    $carerphonenumber = filter_var($_POST["carerphonenumber"],FILTER_SANITIZE_NUMBER_INT);
    $hoodheight = $_POST["hoodheight"];
    $additionalinfo = $_POST["additionalinfo"];     

        // Set the recipient email address
        $recipient = "my.email@website.com";

        // Set the email subject
        $subject = "New Order from $name at $service";

        $br = "<br>";

        echo $name;

        // Input the form information
        $forminformation = "Contact Information:$br$br";
        $forminformation .= "Name: $name$br";
        $forminformation .= "Email: $email$br";
        $forminformation .= "Phone number: $phonenumber$br";
        $forminformation .= "Fire Service: $fireservice$br$br";
        $forminformation .= "Customer information:$br$br";
        $forminformation .= "Name: $customername$br";
        $forminformation .= "Address line 1: $customeraddress1$br";
        $forminformation .= "Address line 2: $customeraddress2$br";
        $forminformation .= "County: $customeraddress3$br";
        $forminformation .= "Postcode: $customeraddress4$br$br";
        $forminformation .= "Carer Information:$br$br";
        $forminformation .= "Name: $carername$br";
        $forminformation .= "Phone number: $carerphonenumber$br$br";
        $forminformation .= "Other Information:$br$br";
        $forminformation .= "Height of hood(cm): $hoodheight$br";
        $forminformation .= "Additional Info: $additionalinfo$br";


        $uid = md5(uniqid(time()));
        $filebasename = basename($file);

        $eol = PHP_EOL;

        // Basic headers
        $header = "From: ".$name." - $fireservice Fire Service".$eol;
        $header .= "Reply-To: ".$email.$eol;
        $header .= "MIME-Version: 1.0".$eol;
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

        // Message section
        $body = "--".$uid.$eol;
        $body .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
        $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
        $body .= $forminformation;
        $body .= $eol.$eol;
        $body .= "--".$uid.$eol;

        // Send the email
        if (mail($recipient, $subject, $body, $header)) {
            // Set a 200 (OK) response code
            header("HTTP/1.0 200 OK");
            echo "Thank you. Your order has been received.";
            unlink($file);
        } else {
            // Set a 500 (internal server error) response code
            header("HTTP/1.0 500 Internal Server Error");
            echo "Sorry, something went wrong, please check all form elements are correctly filled in and try again";
        }

        $confirmationheader = "From: Orders at Website".$eol;
        $confirmationheader .= "Reply-To: orders@website.com".$eol;
        $confirmationheader .= "MIME-Version: 1.0".$eol;
        $confirmationheader .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
        $confirmationmessage = "Thank you for your order with Website. Someone will be in touch shortly";

        mail($email, "Thank you for your order", $confirmationmessage, $confirmationheader);

    } else {
        // Not a POST request, set a 403 (forbidden) response code
        header("HTTP/1.0 403 Server Code - forbidden");
        echo "There was a problem with your submission, please try again.";
    }
?>

Thank you!! Mark

TMG
  • 97
  • 11
  • I think that you need to use the js `FormData` and not serialize all the thing. – Hackerman Nov 09 '16 at 18:44
  • Thanks Hackerman. Would you possibly be able to give me a quick example? Jay Blanchard if you read my post you'll see that actually that's not quite what I'm looking for. – TMG Nov 09 '16 at 21:08

0 Answers0