没有formdata的Ajax文件上传

时间:2018-03-21 19:00:34

标签: php mysql ajax file upload

当我尝试上传文件时,所有数据都发送到数据库但是输入文件是空的并且不会上传文件我怎么能用我的文件发送文件 实际的ajax提交表格? 没有dataForm 我是ajax和mysql的新手

这是我的代码:

  

  function getUsers(){
	$.ajax({
		type: 'POST',
		url: 'tpl/add.php',
		data: 'action_type=view&'+$("#userForm").serialize(),
		success:function(html){
			$('#userData').html(html);
		}
	});
}
function userAction(type,idb){
	id = (typeof idb == "undefined")?'':idb;
	var statusArr = {add:"ajouter",edit:"mises à jour",delete:"supprimer"};
	var userData = '';
	if (type == 'add') { 
		userData = $("#addForm").find('.form').serialize()+'&action_type='+type+'&idb='+idb;
		
	}else if (type == 'edit'){
		userData = $("#editForm").find('.form').serialize()+'&action_type='+type;
	}else if (type == 'view'){ 
		userData = $("#viewForm").find('.form').serialize()+'&action_type='+type;
	}else{
		userData = 'action_type='+type+'&idb='+idb;
	}

	$.ajax({
		url: 'tpl/add.php',
		cache: false,
		data: userData,
		type: 'POST',
		contentType: false,
		processData: false,
		success:function(msg){
			if(msg == 'ok'){
				alert('Le model a été '+statusArr[type]+' avec succès.');
				getUsers();
				$('.form')[0].reset();
				$('#myModal2').modal('hide');
				$('#myModal').modal('hide');
			}else{
				alert('Un probleme à été detecter, veuillez reessayer.');
			}
		}
	});
}
function editUser(idb){
	$.ajax({
		type: 'POST',
		dataType:'JSON',
		url: 'tpl/add.php',
		data: 'action_type=data&idb='+idb,
		success:function(data){
			$('#idbEdit').val(data.idb);
			$('#modelEdit').val(data.model);
			$('#urldemoEdit').val(data.urldemo);
			$('#modelfichEdit').val(data.modelfich);
			$('#etatEdit').val(data.etat);
			$('#editForm').slideDown();
			
		}
	});
}
					<div class="modal fade" id="myModal" role="dialog">
						<div class="modal-dialog">
						  <!-- Modal content-->
						  <div class="modal-content">
							<div class="modal-header">
							  <button type="button" class="close" data-dismiss="modal">&times;</button>
							  <h4 class="modal-title">Ajouter un modele</h4>
							</div>
							<div class="modal-body" id="addForm">
							  <form class="form form-horizontal" method="post" id="userForm" enctype="multipart/form-data">
							  
							<div class="form-group">
								<label class="col-sm-3">Modele HTML</label>
								<div class="col-sm-9">
								<input type="text" class="form-control" name="model" id="model"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3">Aperçu</label>
								<div class="col-sm-9">
								<input type="text" class="form-control" name="urldemo" id="urldemo"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3">Fichier HTML </label>
								<div class="col-sm-9">
								<input type="file" name="file" id="file" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3">Etat</label>
								<div class="col-sm-9">
								<select name="etat" id="etat" class="form-control">
									<option value="0">activé</option>
									<option value="1">désactivé</option>
								</select>
								</div>
							</div>
							
							</div>
							<div class="modal-footer">
							<a href="javascript:void(0);" class="btn btn-success" onclick="userAction('add')">Ajouter</a>
							  <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
							</div>
							</form>
			
						  </div>
						  
						</div>
					  </div>

    

0 个答案:

没有答案