title: Ajax image upload php handling
body: <p>I have two image upload options one for icon and another for image, i have made a custom ajax image upload function in jquery, which send values to php for processing. i am getting image upload to work properly i.e if icon is clicked only icon is uploaded and if image is clicked only image is uploded, my problem is i am getting both value as set in php which is causing error in not selected(icon or image) field.</p>
  
  <p>my codes are:</p>
  
  <p><strong>html</strong></p>
  
  <pre><code>&lt;form name="change" enctype="multipart/form-data"&gt;
  &lt;input name="icon" type="file" /&gt;
  &lt;input name="image" type="file" /&gt;
  &lt;/form&gt;
  
  &lt;div id="icon"&gt;&lt;img src="---" /&gt;&lt;/div&gt;
  &lt;div id="image "&gt;&lt;img src="---" /&gt;&lt;/div&gt;
  &lt;div id="result"&gt;&lt;/div&gt;
  </code></pre>
  
  <p><strong>jquery(Ajax)</strong></p>
  
  <pre><code>$('#icon img').click(function() {
      $('input[name=image ][type=file]').val(null);
      $('input[name=icon][type=file]').trigger('click');
  });
  
  $('#image img').click(function() {
      $('input[name=icon][type=file]').val(null);
      $('input[name=image ][type=file]').trigger('click');
  });
  
  $('input[name=icon], input[name=image ]').change(function() {
      change();
  }); 
  
  
  function change(){
  var formData = new FormData($('form[name=change]')[0]);
  formData.append("CustomField", "This is some extra data");
  $.ajax({
      url: 'upload.php',
      type: 'POST',
      data: formData,
      success: (function(data){
      $('#result').html(data);
      }),
      cache: false,
      processData: false,
      contentType: false
  });
  }   
  </code></pre>
  
  <p><strong>php</strong></p>
  
  <pre><code>if(isset($_FILES['icon']) &amp;&amp; !empty($_FILES['icon']) &amp;&amp; $_FILES['icon'] !== ''){
  echo 'icon set'.'&lt;br /&gt;';
  }
  else{
  echo 'icon not set'.'&lt;br /&gt;';
  }
  
  if(isset($_FILES['image ']) &amp;&amp; !empty($_FILES['image ']) &amp;&amp; $_FILES['image '] !== ''){
  echo 'image set'.'&lt;br /&gt;';
  }
  else{
  echo 'image not set'.'&lt;br /&gt;';
  }
  </code></pre>
  
  <p>If any one icon or image is selected to upload, I am getting result as.</p>
  
  <pre><code>icon set
  image set
  </code></pre>
  
  <p>Please See and suggest what is causing error in php.</p>
  
  <p>Thanks.</p>