Passing form data and file to php using ajax
This question already has an answer here:
Forms by default submit to wherever they're told. In order to stop this, you need to prevent it. Your js should look something like this:
$("form#data").submit(function(event){
event.preventDefault();
...
});
change the submit
type button to button
type and then use AJAX like this:
<button type="button" name="btnSubmit" id="btnSubmit"> Submit </button>
you need to change the jQuery Code to :
$("#btnSubmit").click(function(){
var formData = new FormData($("#myForm"));
$.ajax({
type: 'POST',
url: 'form2.php',
data: formData,
success: function (data) {
alert(data)
},
});
});
and also change the code here if ($_FILES["file"]["error"] > 0)
to if ($_FILES["image"]["error"] > 0)
This line
if ($_FILES["file"]["error"] > 0)
Should be
if ($_FILES["image"]["error"] > 0)
链接地址: http://www.djcxy.com/p/19604.html
上一篇: 用ajax发送输入文件数据
下一篇: 使用ajax将表单数据和文件传递给php