我的表單使用javascript警報與用戶進行通信,因爲這是我工作的公司中的首選方法(而不是不斷的重定向到和來自php處理文件)。通過ajax將文件上傳到php
因此,我通過jQuery中的一些簡單驗證來傳遞所有表單數據,並通過ajax將其發送到php處理程序。這是一個基本看我怎麼做......
var first_name = $(sender + ' #first_name');
var email = $(sender + ' #email');
var tel = $(sender + ' #telephone');
var comments = $(sender + ' #comments');
$.ajax({
type: 'POST',
url: 'sendmail.php',
data: { first_name: first_name.val(),
email: email.val(),
telephone: tel.val(),
comments: comments.val(),
success: function clearFields() {
first_name.val('');
email.val('');
tel.val('');
comments.val('');
alert('Thank you. We will contact you as soon as possible.');
}
}
});
如下與上傳,我無法具有所添加的文件輸入字段來一個這樣的形式。雖然郵件正確地發送,我不認爲Ajax是發送到PHP文件
<input type="file" name="upload" id="upload" />
<script>
var upload = $("#upload");
$.ajax({
type: 'POST',
url: 'sendmail.php',
data: { first_name: first_name.val(),
email: email.val(),
telephone: tel.val(),
upload: upload.val(),
comments: comments.val(),
success: function clearFields() {
first_name.val('');
email.val('');
tel.val('');
upload.val('');
comments.val('');
alert('Thank you. We will contact you as soon as possible.');
}
}
});
</script>
我發現了許多用於此選項上傳任何可用的數據,但所有那些我」我看着such as this對我來說似乎是「黑客」。
有沒有更簡單的方法來做到這一點?
是不是要使用jQuery Form? http://malsup.com/jquery/form/ – alexbusu 2013-02-27 08:40:08