我的表单使用 javascript 警报与用户交流,因为这是我工作的公司的首选方法(与不断重定向到 php 处理程序文件和从 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>
我为此找到了许多选项,但是我看过的所有选项对我来说似乎都是“骇人听闻的”。
有没有更简单的方法来做到这一点?