【问题标题】:POSTing data serverside AND execute javascript code on submitting a form在服务器端发布数据并在提交表单时执行 javascript 代码
【发布时间】:2013-02-16 10:57:52
【问题描述】:

我的目标是将一些图像上传到服务器并为它们提供描述。

点击上传按钮后,这是我想要发生的事情:

1) javascript 函数动态添加表单以获取描述 图片。

2) 提交表单时:

 a) the description entered in the form must be available $_POST['description'] at server side.
 b) the images are sent to the server using an XMLHttpRequest

在我编写的代码中,描述不可用$_POST['description']。 当我删除检查if(!isset($_POST['description'])) 时,图像文件已完美上传。 这是我的代码:

javascript 代码

upload.onclick = uploadPrompt;

// dynamically add a form
function uploadPrompt () {
    // fileQueue is an array containing all images that need to be uploaded
    if (fileQueue.length < 1) {
        alert("There are no images available for uploading.");
    } else {
        var inputDescription = document.createElement("input");
        inputDescription.className = "promptInput";
        inputDescription.type = "text";
        inputDescription.name = "description";

        var inputButton = document.createElement("button");
        inputButton.id = "promptInputButton";
        inputButton.type = "submit";
        inputButton.innerHTML = "Start uploading";

        var promptForm = document.createElement("form");
        promptForm.method = "post";
        promptForm.action = "upload.php";
        promptForm.onsubmit = uploadQueue;
        promptForm.id = "promptForm";
        promptForm.appendChild(inputDescription);
        promptForm.appendChild(inputButton);

        document.body.appendChild(promptForm);
    }
}

function uploadQueue(ev) {
    ev.preventDefault();

    elementToBeRemoved = document.getElementById("promptForm");
    elementToBeRemoved.parentElement.removeChild(elementToBeRemoved);

    while (fileQueue.length > 0) {
        var item = fileQueue.pop();
        // item.file is the actual image data
        uploadFile(item.file);
    }
}

function uploadFile (file) {
    if (file) {
        var xhr = new XMLHttpRequest();

        var fd = new FormData();
        fd.append('image',file);

        xhr.upload.addEventListener("error", function (ev) {
            console.log(ev);
        }, false);

        xhr.open("POST", "upload.php");

        xhr.setRequestHeader("Cache-Control", "no-cache");
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.setRequestHeader("X-File-Name", file.name);

        xhr.send(fd);
    }
}

php代码上传.php

<?php

session_start();

if (!isset($_POST['description'])) {
    echo "upload:fail\n";
    echo "message:No scene was specified";
    exit();
}


if (isset($_FILES['image'])) {
    if(!move_uploaded_file($_FILES['image']['tmp_name'], "uploads/" . $_POST['description'] . "/" . $_FILES['image']['name'])) {
        echo "upload:fail\n";
        }
    else {
        echo "upload:succes\n";
    }
    exit();
}

exit();
?>

【问题讨论】:

    标签: javascript forms post


    【解决方案1】:

    当有大量开发人员已经更好地编写了相同的东西时,我真的建议不要创建自己的异步文件上传功能。查看以下选项:

    这两个我以前用过,效果很好。对于 BlueImp,您可以使用此选项发送额外的表单数据:

    $('#fileupload').fileupload({
       formData: $('.some_form').serialize()
    });
    

    上面捕获了一个表单并序列化了它的输入。或者,您可以使用特定值(即来自 DOM 中的特定元素)填充数组或对象:

    var array = new Array();
    $('.description').each(function() {
       array[this.id] = this.value;
    });
    

    您可以使用 ID 来链接您的文件和描述。

    【讨论】:

    • 我意识到我的代码是垃圾,我知道你建议的选项。虽然上传只是我正在尝试制作的应用程序的一部分。您建议的“序列化()”方法对我帮助很大!谢谢!!
    • 您还有什么需要帮助的吗?否则,您可以通过接受答案将此问题标记为已关闭 - 这将有助于您的接受率
    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多