I have a form to upload files with 2 parameters (type = "file", type = "text") to pass through ajax in PHP. Looking at google I found the script you see below. The script works perfectly and also visualize the progress bar, but I can spend only what concerns the parameter type = "file" but I can add to what I write in the field type = "text".
<script>
function _(el){
return document.getElementById(el);
}
function uploadFile(){
var file = _("userfile").files[0];
var formdata = new FormData();
formdata.append("userfile", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "TEST.php");
ajax.send(formdata);
}
function progressHandler(event){
_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
location.href="home.php";
}
function errorHandler(event){
_("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
_("status").innerHTML = "Upload Aborted";
}
</script>
<form id="upload_form" enctype="multipart/form-data" method="post">
<input class="formUpDes" type="text" name="description" id="description">
<input class="formUpFile" type="file" name="userfile" id="userfile">
<input class="formUpBt" type="button" value="upload" onclick="uploadFile()">
<progress class="formUpProg" id="progressBar" value="0" max="100"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>
I would just add the text that is written in the text field in the function UploadFile() so that it gets passed to the PHP file.
please help me