如何将文件上传的输入文本发送到ajax请求中的upload.php以及如何接收

时间:2017-02-24 14:43:43

标签: javascript php ajax

如何将表单门中的文字添加到书名[{1}}中,并将其发送到id:bname页面,以及如何在upload.php页面中接收文字。

upload.php

上传文件中的功能

function uploadFile(){     
    var file = document.getElementById("upload").files[0];  
    var formdata = new FormData();  
    formdata.append("upload", 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", "upload.php");  
    ajax.send(formdata);  
    clikedbtn();
}  

html标签

function progressHandler(event){  
    var percent = (event.loaded / event.total) * 100;  
    document.getElementById("progressBar").value = Math.round(percent);  

}    
function completeHandler(event){  
    document.getElementById("progressBar").value = 0;
}
function errorHandler(event){}
function abortHandler(event){}
function clikedbtn(){}

upload.php的
我想使用我将在此页面中发送的文本

<form method="post" enctype="multipart/form-data">
    <label for="bname">book name</label>
    <input name="bname" id="sname" type="text">
    <label for="dbname">bood desc</label>
    <input name="dbname" id="dbname" type="text">
    <input id="upload" name="upload" type="file">
    <input id="btnupload" type="button" value="add" onclick="uploadFile() ">
</form>

1 个答案:

答案 0 :(得分:0)

收到回复:

  var ajax = new XMLHttpRequest();// under this line 
  ajax.onreadystatechange = function() {
        if (ajax.readyState === XMLHttpRequest.DONE && ajax.status === 200) {
            // you can hanle ajx.responseText 
        }
    };