如何使用AJAX传递下一页中的多个文件值

时间:2017-02-15 14:31:06

标签: javascript php jquery

这里我有两个表单字段,一个是文本字段,另外一个是文件上传,现在我想做所有字段值并通过AJAX传递到下一页,这里文本框值传递但文件上传值不是传递我得到错误像undefined undefined,如何做到这一点

  

的console.log(结果);

我得到这样的答案

    Array
(
    [Price_unit] => Array
        (
            [0] => 4000000
            [1] => 8000000
        )

    [floor_plan] => Array
        (
            [0] => undefined
        )

     )

$(document).ready(function () {
  $("#btn-submit").click(function (){
    var formData = new FormData($('#residential')[0]);
    formData.append('floor_plan[]', $('input[type=file]')[0].files[0]);

    $.ajax({
      url: "admin/test.php", 
      type: "POST",          
      data: formData, 
      contentType: false,     
      cache: false,             
      processData:false,        
      success: function(result)   
      {
        console.log(result);

      }
    }); 

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="residential">
  <div class="form-group">
    <div class="row filterhide1" style="margin-left: 0px;">


      <div class="col-md-3">	
        <div class="form-group">
          <label class="control-label">Price

            <span class="require">*</span>
          </label>
          <br>
          <input type="text" class="form-control" placeholder="" name="Price_unit[]" value="4000000" required="" data-msg-required="Please Enter Price" aria-required="true">
        </div>	
      </div>
      <br>
      <div class="col-md-3">	
        <div class="form-group">
          <label class="control-label">Floor Plan

            <span class="require">*</span>
          </label>
          <br>
          <input type="file" class="" placeholder="" name="floor_plan[]" value="luxury_apartments_and_property.jpg" required="" data-msg-required="Please Select Floor Plan" aria-required="true">
        </div>	
      </div>
    </div>
  </div>


  <br><br><br><br>

  <div class="form-group">
    <div class="row filterhide1" style="margin-left: 0px;">


      <div class="col-md-3">	
        <div class="form-group">
          <label class="control-label">Price

            <span class="require">*</span>
          </label>
          <br>
          <input type="text" class="form-control" placeholder="" name="Price_unit[]" value="8000000" required="" data-msg-required="Please Enter Price" aria-required="true">
        </div>	
      </div>
      <br>
      <div class="col-md-3">	
        <div class="form-group">
          <label class="control-label">Floor Plan

            <span class="require">*</span>
          </label>
          <br>
          <input type="file" class="" placeholder="" name="floor_plan[]" value="6.jpg" required="" data-msg-required="Please Select Floor Plan" aria-required="true">
        </div>	
      </div>
    </div>
  </div>
  <br><br>

  <input type="button" value="SUBMIT" id="btn-submit">
</form>

  

test.php的

 <?php
print_r($_POST);
?>

0 个答案:

没有答案