Ajay
Ajay

Reputation: 1117

problem in sending form data with Uploadify

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.

Upvotes: 2

Views: 3172

Answers (2)

bzarah
bzarah

Reputation: 358

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.

Upvotes: 1

S L
S L

Reputation: 14318

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',

Upvotes: 0

Related Questions