1

我有一个 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 doc,我只是想看看文件是否正在上传

    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;
    }
}
4

1 回答 1

0

你的<form action="...">需要被删除。您正在使用 AJAX,因此一起绕过了表单操作序列。我在我的服务器上创建了一个副本,没有它就可以工作。

我建议不时将其放入 JSFiddle 并点击顶部的“Tidy”按钮。否则,到目前为止,代码做得很好。

如果您想查看我的复制副本, 请单击此处

于 2013-10-26T09:40:13.513 回答