2015-08-26 27 views
0

我無法在PHP AJAX請求發送文件:無法發送文件中PHP AJAX請求

形式:

<form class="frmContact" action="#" method="post"> 
        <div class="col-md-6"> 
        <input type="hidden" name="emailTo" id="emailTo" value="[email protected]">  
        <input type="text" placeholder="Name" name="txtName" id="txtName"> 
        <input type="email" placeholder="Email" name="txtEmail" id="txtEmail"> 
        <input type="text" placeholder="Subject" name="txtSubject" id="txtSubject"> 

        <input type="file" placeholder="Text File" name="txtFile" id="txtFile" /> 

        </div> 
        <div class="col-md-6"> 
        <textarea placeholder="Message" cols="40" rows="10" name="txtText" id="txtText"></textarea> 
        <button class="btnSend" type="button">Send Your Message</button> 
        <span id="spanMessage"></span> 
        </div> 
       </form> 

Ajax代碼:

$.ajax({ 
     url: "includes/mail.php", // Url to which the request is send 
     type: "POST",    // Type of request to be send, called as method 
     data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values) 
     contentType: false,  // The content type used when sending data to the server. 
     cache: false,    // To unable request pages to be cached 
     processData:false,  // To send DOMDocument or non processed data file it is set to false 
     success: function(data) // A function to be called if request succeeds 
     { 
     console.log(data); 
     } 
     }); 

PHP代碼:

echo '<pre>'; print_r($_REQUEST); print_r($_FILES); die; 

控制檯結果:

Array 
(
) 

Array 
(
) 

使用jQuery的jQuery v1.11.2

能否請你幫我調試的錯誤?

+4

那麼,您究竟在哪裏發送文件到服務器? –

+0

@TobiasKun我無法讀取文件。接收空$ _FILES –

+0

爲什麼不使用:data:$(form).serialize()而不是數據:new FormData(this)? –

回答

2

您忘記了enctype="multipart/form-data"。請將此添加到表單標籤:

<form class="frmContact" action="#" method="post" enctype="multipart/form-data"> 
+0

Did not與那 –

+0

@KunwarbirS。控制檯中有任何錯誤? –

+0

在控制檯中沒有錯誤 –