title: Ajax - Sending input file and additional variable to php file through ajax
body: <p>I'm trying to send <code>input file</code> and <code>input text</code> through ajax. Because I'll add this feature to my existing function that has plenty of other variables I cannot simply use sending the entire <code>Form</code> like the one used here </p>
  
  <p><a href="https://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax">Uploading both data and files in one form using Ajax?</a></p>
  
  <p>This is the basic gist of it </p>
  
  <p>My HTML </p>
  
  <pre><code>&lt;input type='text' name='text' id='text'&gt;
  &lt;input type='file' name='media' type="file" / id='media'&gt;
  &lt;input type="button" value="Upload" name='submit'/&gt;
  </code></pre>
  
  <p>My Ajax</p>
  
  <pre><code>$(":button").click(function(){
          var myFormData = new FormData();
          var media = document.getElementById('media');
          var variable = 'foo';
          myFormData.append('pictureFile', media.files[0]);
          var text = $("#text").val();
          $.ajax({ 
              type: 'POST',
              url: 'upload.php',
              data: 
              {
                  pictureFile: myFormData,
                  text: text,
                  var: variable,
              },  
              cache: false,
              success: function (data) {
                  alert(data);
              },
              processData: false,
              contentType: false, 
          });
  }); 
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>include_once ("connection.php");
  if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  
      $temp_name = $_FILES['pictureFile']['name'];
      $pic_type = $_FILES['pictureFile']['type'];
  
      $pic_temp = $_FILES['pictureFile']['tmp_name'];
      $pic_path = "images/";
      move_uploaded_file($pic_temp,'images/'.$temp_name);
  
  }
  </code></pre>
  
  <p>So as shown in my code I need a way to send those <code>var media</code>, <code>var text</code> and <code>var variable</code> from <code>data:{},</code> to <code>upload.php</code></p>