title: Passing form data and file to php using ajax
body: <p>This might have been asked before, but i have search on here and on google and every answer I have read doesnt work.</p>
  
  <p>The question I have to solve is make a form with first name, last name, email and a image. Then pass the data into a database and upload the file also to a database. Currently my code doesnt do anything after I press submit. Before I added the file box it would insert the data into my database.</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;form id="myForm" method ="post" enctype="multipart/form-data"&gt;
      First Name: &lt;input type="text" name="fname" id="fname"&gt; &lt;br&gt;
      Last Name: &lt;input type="text" name="lname" id="lname"&gt; &lt;br&gt;
      Email:  &lt;input type="text" name="email" id="email"&gt; &lt;br&gt;
      Image: &lt;input type="file" name="image" id="image"&gt; &lt;br&gt;
      &lt;button type="button" name="btnSubmit" id="btnSubmit"&gt; Submit &lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>AJAX/JS</strong></p>
  
  <pre><code>$("#btnSubmit").click(function(){
       var formData = new FormData($(this)[0]);
       $.ajax({
          type: 'POST',
          url: 'form2.php',
          data: formData,
           success: function (data) {
             alert(data)
           },
        });
    });
  </code></pre>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>$upload = basename($_FILES['image']['name']);
  $type = substr($upload, strrpos($upload, '.') + 1);
  $size = $_FILES['image']['size']/1024; 
  
  if ($_FILES["image"]["error"] &gt; 0)
  {
      echo "Error: " . $_FILES["image"]["error"] . "&lt;br&gt;";
  }
  else
  {
      echo "Upload: " . $upload . "&lt;br&gt;";
      echo "Type: " . $type . "&lt;br&gt;";
      echo "Size: " . $size . " kB&lt;br&gt;";
  }
  
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $email = $_POST['email'];
  echo "You Entered &lt;br /&gt;";
  echo "&lt;b&gt;First Name:&lt;/b&gt; ". $fname . "&lt;br /&gt;";
  echo "&lt;b&gt;Last Name:&lt;/b&gt; ". $lname . "&lt;br /&gt;";
  echo "&lt;b&gt;Email:&lt;/b&gt; ". $email . "&lt;br /&gt;";
  </code></pre>