problem in sending form data with Uploadify

我的梦境 提交于 2019-12-10 23:44:09

问题


I am trying to send form's additional data with uploadify using scriptData but it does not send anything to backend PHP script. Can someone help me with it please? Here is what I'm trying..

Javascript:

<script type="text/javascript">
$(document).ready(function() {
    $("#fileUpload").fileUpload({
        'uploader': 'uploadify/uploader.swf',
        'cancelImg': 'uploadify/cancel.png',
        'script': 'uploadify/upload.php',
        'folder': 'files',
        'multi': false,
        'displayData': 'speed',
        'scriptData' : {'name' : $('#name').val()},
        onComplete: function (evt, queueID, fileObj, response, data) {
            alert("Successfully uploaded: "+response);
        }
    });
});
</script>

HTML:

Name: <input name="name" id="name" /><br />
<div id="fileUpload">You have a problem with your javascript</div>
<a href="javascript:$('#fileUpload').fileUploadStart()">Start Upload</a> |  <a href="javascript:$('#fileUpload').fileUploadClearQueue()">Clear Queue</a>

upload.php

(I have tried both POST and GET methods, none works).

$name = $_GET['name'];
or
$name = $_POST['name'];

I'll be very thankful for your help.


回答1:


I know it's a little late, but I was trying to get this to work myself, and attempted to do the same thing you did.

You can't use the jQuery selector to get the value of "Name" in the initial setup of Uploadify:

'scriptData' : {'name' : $('#name').val()}

It's a timing issue. At the time the jQuery selector is evaluated, the page has just finished loading, and your 'Name' textbox (I assume) is empty. It does not get re-evaluated when the upload occurs.

You can, however, update the scriptdata at anytime, including just before the upload occurs:

$('#myUpload').uploadifySettings('scriptData', {'Id':$('#IdTextBox').val() });
$('#myUpload').uploadifyUpload();

That way, you can extract form data and pass it along with the upload. This is the simplest example, it gets more complicated with multiple file uploads, but that should be enough to get someone started.




回答2:


I think you forgot 'fileDataName' : 'photo'

Also you don't have 'method' : 'post' option, you can access scrpitData as GET or POST according to it.

And on the back-end, you can access files by $_FILES['photo'];

Also you need to check that these files exists in their relative path.

'uploader': 'uploadify/uploader.swf',
'cancelImg': 'uploadify/cancel.png',
'script': 'uploadify/upload.php',


来源:https://stackoverflow.com/questions/5563200/problem-in-sending-form-data-with-uploadify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!