Javascript Post多部分/表单数据(图像和文本)html-Php-SQl-AJAX
我对代码进行了更改Javascript Post多部分/表单数据(图像和文本)html-Php-SQl-AJAX,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我对代码进行了更改 <html> <body> <form action="test.php" id="subscriber_form" method="post" enctype="multipart/form-data"> <input type="text" id="title" name="title" ><br> <input type="file" id="file" name="file" >&
<html>
<body>
<form action="test.php" id="subscriber_form" method="post" enctype="multipart/form-data">
<input type="text" id="title" name="title" ><br>
<input type="file" id="file" name="file" ><br>
<input type="submit" id="submit" value="submit">
</form>
<div id="show_subscriber_msg"></div>
<script src="js/jquery-1.7.1.min.js"></script>
<script>
$("form#subscriber_form").submit(function(){
$('#show_subscriber_msg').html('<div class=gen>Submiting..</div>');
e.preventDefault();
var formURL = $(this).attr("action");
var formData = new FormData($(this)[0]);
$.ajax({
url: formURL,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
async: false,
success: function (res) {
if(res=='1'){
$('#show_subscriber_msg').html('<div class=gen>Thank you</div>');
}
if(res=='5'){
$('#show_subscriber_msg').html('<div class=err>Please enter a valid email address</div>');
}
}
});
});
</script>
</body>
</html>
$(“表单#订户表单”)。提交(函数(){
$('#show_subscriber_msg').html('提交..');
e、 预防默认值();
var formURL=$(this.attr(“操作”);
var formData=新formData($(此)[0]);
$.ajax({
url:formURL,
键入:“POST”,
数据:formData,
cache:false,
contentType:false,
processData:false,
async:false,
成功:功能(res){
如果(res='1'){
$('#show_subscriber_msg').html('谢谢');
}
如果(res='5'){
$('#show_subscriber_msg').html('请输入有效的电子邮件地址');
}
}
});
});
我的sql表正在更新,文件已正确上载到目录
但它仍然将我重定向到test.php。我只想在后台运行我的php脚本,即
test.php
您必须更改表单元素,将编码类型更改为多部分/表单数据
<form id="subscriber_form" method="post" enctype="multipart/form-data">
至于后端部分,我对PHP不太感兴趣。如果您需要Java代码,我可以帮助您。此外,这个答案可能会帮助您找到解决方案,因此data:formData将自动从我的表单中获取post数据,包括文件。。。我说得对吗?是的。如果您觉得它有用,请选择它作为答案。当我单击“提交”时,我的页面正在刷新。。。。我想在后台发布数据。。我的意思是不刷新我的页面;在答案中进行了更改,请进行检查。
$("form#subscriber_form").submit(function(e){
e.preventDefault();
var formURL = $(this).attr("action");
var formData = new FormData($(this)[0]);
$.ajax({
url: formURL,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false
async: false,
success: function (data) {
console.log(data);
}
});