【发布时间】:2013-10-25 18:17:34
【问题描述】:
我有一个 html 表单,当用户选择一个文件并提交它时,php 文档中的 $_FILES 没有收到它。主要焦点是表单,当我点击提交时,没有文件被提交,但 onclick 事件会触发。并且没有错误信息
<form id="artist_post" enctype="multipart/form-data" method="post" onsubmit="return " action="php_parsers/newsfeed_system.php">
<textarea id="statustext" onkeyup="statusMax(this,250)"></textarea>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input id="audioUpload" type="file" name="audioUpload" accept="audio/*" />
<input type="submit" value="Post" id="statusBtn" onclick="postToStatus('status_post','a','statustext')"/>
</form>
这是 php 文档,我只是想看看文件是否正在上传
if (isset($_FILES["audioUpload"]["name"])) {
echo "flag1";
}
if ($_FILES["audioUpload"]["tmp_name"] != "") {
echo "flag2";
}
javascript从表单中获取信息并通过ajax发送给php。
function postToStatus(action,type,ta){
var data = _(ta).value;
if(data == ""){
alert("Type something first");
return false;
}
// Checks to see if user uploaded a file to send with post
if(_("audioUpload").value != "") {
type = "c";
}
_("statusBtn").disabled = true;
var ajax = ajaxObj("POST", "php_parsers/newsfeed_system.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
//Pull some info from whatever the php doc returns
}
}
ajax.send("action="+action+"&type="+type+"&data="+data);
}
function ajaxObj(meth, url) {
var x = new XMLHttpRequest(); // create new http request
x.open( meth, url, true ); // open request and pass it a method and a url to post it to
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
// Modular function that externalizes readyState and status check
function ajaxReturn(x) {
if(x.readyState == 4 && x.status == 200) {
return true;
}
}
【问题讨论】:
-
你的javascript
postToStatus()函数包含什么? -
首先取出
onsubmit="return "和onclick="postToStatus()",除非你正确调用那个/那些函数。 -
您是否尝试通过 javascript 发布文件?因为没有 jquery lib 就无法工作
-
@JerkoW.Tisler “没有 jquery 库”;你知道 jquery 是用 in javascript 编写的,而且你可以用 jquery 做的所有事情都可以,因此,不用它(在原始 javascript 中)?
-
是的,你可以在 JS 中做到这一点。我的意思是,你是对的 JQuery 是用 javascript 编写的,但老实说,我真的怀疑你写了 1000 多行代码来用原生 JS 上传文件
标签: php forms file-upload