我正在尝试通过ajax发送附件到php的表单数据。
我的代码是:
var company_name = $('#companyname').val();
var company_type = $('#companytype').val();
var industry_sector = $('#industrysector').val();
var email = $('#email').val();
var mobile = $('#mobile').val();
var logo = $("#logo").prop("files")[0];
var company_description = $('#company_description').val();
var password = $('#password').val();
var rePassword = $('#rePassword').val();
var data = {
company_name: company_name,
company_type: company_type,
industry_sector: industry_sector,
email: email,
mobile: mobile,
logo: logo,
company_description: company_description,
password: password
}
$.ajax({
type: "POST",
async: false,
data: {companyregister: data},
url: "ajax/loginvia.php",
processData: false, // tell jQuery not to process the data
contentType: false,
beforeSend:function(){
},
我得到空数据到php控制器。我试过很多方面,我无法解决这个问题。
答案 0 :(得分:0)
使用简单的ajax,执行起来很复杂,你可以使用ajaxForm。它很容易实现。这是插件网址:http://jquery.malsup.com/form/
这很容易实现,看看这个。
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>
答案 1 :(得分:0)
$.ajax({
url: "ajax_php_file.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
$('#loading').hide();
$("#message").html(data);
}
});
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/".$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file