title: Uploading file via ajax
body: <p>i am trying to upload file using ajax.</p>
  
  <pre><code>form enctype="myltipart/form-data" id="pastTest-form" method="POST" action="upload.php"&gt;
              &lt;div class="feedback-form-inputs col-5"&gt;
                  &lt;div class="input-field"&gt;
                      &lt;select id="type" required&gt;
                          &lt;option value="quiz"&gt;Quiz&lt;/option&gt;
                          &lt;option value="midterm"&gt;Midterm&lt;/option&gt;
                          &lt;option value="final"&gt;Final&lt;/option&gt;
                      &lt;/select&gt;
                  &lt;/div&gt;
                  &lt;div class="input-field"&gt;
                      &lt;input type="text" id="professor"/&gt;
                  &lt;/div&gt;
                  &lt;div class="input-field"&gt;
                      &lt;input type="text" id="name"/&gt;
                  &lt;/div&gt;
                  &lt;div class="input-field"&gt;
                      &lt;input type="file" id="uploaded_file" name="file" accept="" required /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;&lt;!-- END feedback-form-inputs --&gt;
              &lt;div class="clear"&gt;&lt;/div&gt;
                  &lt;input type="submit" value="submit" onclick="submit() /&gt;
                  &lt;div id="upload-status"&gt; &lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>my function for opening ajax are in the external file.</p>
  
  <pre><code>function addPastTest1(cid){
      // form variables
      var type = _("type").value;
      var professor = _("professor").value;
      var name = _("name").value;
  
      var fileSelect = _('uploaded_file');
      var status = _('upload-status');
      event.preventDefault();
      // Update status text.
      status.innerHTML = 'Uploading...';
      // Get the selected files from the input.
      var file = fileSelect.files[0];
  
      var FileName = file.name;
      var FileSize = file.size;
      var allowed = ["msword", "pdf","pages"];
      var found = false;
      // check if the extension of the file match the allowed ones
      allowed.forEach(function(extension) {
          if (file.type.match('application/'+extension)) {
            found = true;
          }
      })
      if (FileSize &gt;10204){
          status.innerHtml = 'File must be less than 1mb in size';
      }
      if (found==true){
          // Create a new FormData object.
          var formData = new FormData();
          // Add the file to the request.
          formData.append('file', file, FileName);
          // Set up the request.
          var ajax = ajaxObj("POST", "ajaxResponse.php");
          ajax.onreadystatechange = function(){
              if (ajaxReturn(ajax)==true){
                  if (ajax.responseText=='failed'){
                  status.innerHtml = "failed to upload file";
                  }
                  else {
                      status.innerHtml = 'uploaded';
                      alert(ajax.responseText);
                  }
              }
          }
          ajax.send(formData);            //ajax.send("f="+formData+"&amp;t="+type+"&amp;p="+professor+"&amp;n="+name+"&amp;cid="+cid+"&amp;fn="+FileName);
      }
  }
  </code></pre>
  
  <p>so i am sending formData to the php. but at this point i can not take the file from form data and upload it to the server.
  here is my php</p>
  
  <pre><code>// Ajax calls this code to add a past test
  if (isset($_FILES['file']){
      $file = $_FILES['file'];
              $path = 'files/'.$type.'/'.$fileName;
              $moveResult = move_uploaded_file($file, $path);
  
          if ($moveResult != true) {
              echo "ERROR: File not uploaded. Try again.";
              //unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
              exit();
          }
  
  
      $path = 'files/'.$type.'/'.$fileName;
      $sql = "INSERT into past_papers VALUES ('$name', '$type', '$cid', '$professor','$path')";
      $query = mysqli_query($db_conx,$sql);
      if (mysqli_affected_rows($db_conx)&gt;0){
          echo "success";
          exit();
          }   
      else {
          echo "failed sql";
          exit();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Also i want to grab inputs with the file and process them together. Upload file, and insert inputs into database.</p>