MrAntunes
11/28/2017 - 9:23 AM

UploadFile "Assync" without Ajax

form to upload files, by normal POST, without new page load (target: invisivle frame)

<form class="form-horizontal" method="POST" action="<?php echo CFG_APPLICATION_ROOT; ?>/visits/edit/"
					  id="formvisit" onsubmit="return false;" target="iframeUpFiles"
					  enctype="multipart/form-data">
					  
					  
<iframe name="iframeUpFiles" id="iframeUpFiles"  style="display: none"></iframe>


//trigger buts clicks
on("click", ".btpost", function(e)
	{
		//save/atachDoc/delete visit
		pageLoadingFrame("show");
		
		////assign method for distinctions on Controller
		let t = $(this), _method = t.data("method");
		$("#formvisit").find("[name='_method']").val(_method);
		
		if(_method === 'saveattach')
		{
			$("#formvisit").find("[name=\'_method\']").val(_method);
			$("#formvisit").get(0).submit();
			
			//after >> $("#iframeUpFiles").on("load"
		}



//hack for refresh page after file upload
	$("#iframeUpFiles").on("load", function(e)
	{
	 	let params = getUrlParams('idvisita');
		//se nova visita, descobrir ID da nova
		if(params.idvisita === undefined)
		{
			pageLoadingFrame("show");
		
			$.ajax(
			{
				url: ROOT + "/visits/edit/loader.php?_method=getlastvisit",
				type: "POST",
				dataType: "html",
				data: $("#formvisit").serialize(),
				success: function(response)
				{
					pageLoadingFrame("hide");
					document.location.href = document.location.href + "&idvisita=" + response;
				},
				error: function()
				{
					pageLoadingFrame("hide");
					parent.location.reload();
				}
			});
		}
		else document.location.href = document.location.href;
	});