0

I'm currently working on a website which mail user attachments to the owner of the website. However I used a form to submit image files to mail PHP file and then it send the email. It worked perfectly until I override form submit function with Javascript and use ajax to submit it and get the output from mail PHP file to add some additional functionalities.

<form id="contactForm" action="php/sendcontactusmail.php" method="post" enctype="multipart/form-data">
    <div class="row">
        <div class="col-md-6">
        <!-- Additional Codes for email input and subject inputs -->
            <div class="form-group">
                <label for="subject" id="uploadImageLabelId">Upload Pictures of the Job</label>
                <form  enctype="multipart/form-data" >
                    <input type="file" name="fileToUpload" id="imageUploadInputId" accept="image/*" id="fileToUpload" multiple>
                </form>
            </div>
            <!-- Additional Codes for Message inputs and others -->
            <div class="col-md-12">
                <button type="submit"class="btn btn-primary pull-right" id="btnContactUs">Send Message</button>
            </div>
      </div>
   </div>
</form>

Javascript code:

$("#contactForm").submit(function(){
    $.ajax({
        type:"POST",
        url : "php/sendcontactusmail.php",
        dataType:"json",
        data: $("#contactForm").serialize(),
        success: function (data)
        {
            alert(data);    
        }

    });
    return false;
});     

**My Problem **

PHP code:

if(filter_var($_POST["subject"], FILTER_SANITIZE_STRING)=="estimate")
{
    if($_POST && isset($_FILES['fileToUpload']))
    {       
        // Some Codes to send email
    }
    else 
        echo json_encode(isset($_FILES['fileToUpload']));
}
else
{
    // Some codes for send separate email.
} 

when I run my program, alert box shows false. Which clearly means that isset($_FILES['fileToUpload']) function returns false. I tried every way to fix that. But couldn't find the problem. Any guesses?

Note

When I run the website on my local server I selected a compatible jpg file. But it says file is not set.

Update

I went through lots of stackoverflow questions and none of their solutions gave me a true return statement. Hope this wouldn't be a duplicated question.

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
sam
  • 1
  • 3
  • Your title says that `$_FILES['attName']` but cant find that in your code – Carsten Løvbo Andersen Jun 06 '17 at 06:33
  • Possible duplicate of: https://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload – Oliver Jun 06 '17 at 06:34
  • @CarstenLøvboAndersen I did that to make the title more general. Sorry if that iswrong. I'm new to stackoverflow. – sam Jun 06 '17 at 06:41
  • @Oliver Actually what it suggest is not a solution for me since that doesn't return a true from isset. That's why I asked this question. – sam Jun 06 '17 at 06:43

0 Answers0