0
我有一個簡單的表單,它將通過AJAX發送到一個php文件進行處理,這個表單包含一個文件上傳,由於某種原因沒有包含文件上傳在要發送的數據中。文件輸入類型不包含在jQuery FormData for AJAX中發送
我的HTML表單:
<form id="business-form">
<label for="business-name">Business Name:</label>
<input type="text" id="business-name" name="business-name">
<label for="business-location">Business Location:</label>
<input type="text" id="business-location" name="business-location">
<label for="business-description">Business Description:</label>
<input type="text" id="business-description" name="business-description">
<label for="fileToUpload">Business image:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<button id="create-business">Create New Business</button>
</form>
我的jQuery:
function createNewBusiness(){
var businessName = $('#business-name').val();
var businessLocation = $('#business-location').val();
var businessDescription = $('#business-description').val();
var file_data = $('#fileToUpload').prop('files')[0];
var sendAJAX = false;
if (businessDescription.length > 1 && businessLocation.length > 1 && businessName.length > 1) sendAJAX = true;
if (sendAJAX) {
var formData = new FormData($("#business-form")[0]);
var ft = $('#fileToUpload')[0].files[0];
formData.append("fileToUpload", ft);
console.log(ft);
console.log(formData);
$.ajax({
url: '/app/business/save_created', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: formData,
type: 'post',
success: function (php_script_response) {
alert(php_script_response); // display response from the PHP script, if any
}
});
}else{
alert("Please fill in all fields to create a new business")
}
}
PHP腳本返回:
Array ([business-name] => fefe [business-location] => fe [business-description] => fe)
當選擇了一個文件,但該文件是空白我得到:
Array ([business-name] => dede [business-location] => eafrea [business-description] => aefad [fileToUpload] => undefined)
的(濾餅)的PHP當前處理AJAX:
public function app_save_created(){
$this->autoRender = false;
$this->layout = 'ajax';
print_r($this->request->data);
}
您需要表單標籤屬性中的多部分表單數據。 –