[英]How can I send input text with the file upload to upload.php in ajax request and how to receive
How can I add text from the form gate like book name id:bname
to the request to send it to upload.php
page and how I can receive the text in upload.php
page. 如何在表单门id:bname
书名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();
}
Functions in upload file 上传文件中的功能
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(){}
html tags html标签
<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>
upload.php upload.php的
I want to use the text that I will send in this page 我想使用将在此页面中发送的文本
<?php
$fileName = $_FILES["upload"]["name"];
$fileTmpLoc = $_FILES["upload"]["tmp_name"];
$fileType = $_FILES["upload"]["type"];
$fileSize = $_FILES["upload"]["size"];
$fileErrorMsg = $_FILES["upload"]["error"];
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "upload/$fileName")){
echo "$fileName upload is complete";
} else {
echo "move_uploaded_file function failed";
}
?>
For receiving the response: 接收响应:
var ajax = new XMLHttpRequest();// under this line
ajax.onreadystatechange = function() {
if (ajax.readyState === XMLHttpRequest.DONE && ajax.status === 200) {
// you can hanle ajx.responseText
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.