0

I'm using JQuery Form Plugin for AJAX file uploader.

The (html) form is created dynamically, and looks like this:

<form id="formUpload" action="fileReceiver.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileUpload" multiple>
    <input type="submit" value="Upload File to Server">
</form>

Because, the form is created dynamically, I'm using jquery on(). I also need to send a few variables, I'm using data options from the plugin.

The Javascript looks like this:

$(document).on("submit", "form#formUpload", function() {
    $(this).ajaxForm({
        data: { someVariable : 'someValue' },
        complete: function(xhr) {
            status.html(xhr.responseText);
        }
    }); 
});

I think the form is binded correctly, I could call/alert something from the ajaxForm (jquery form plugin) function through beforeSend or Success options.

Now, the problem is the PHP couldn't get the data I posted in the Javascript.

My PHP is simple like this:

<?php
   echo $_POST["someVariable"];
?>

It gives me error "Notice: Undefined index: someVariable blah blah blah"

Any advice? Thx :)

rooivalk
  • 177
  • 3
  • 15

3 Answers3

0

Try adding some variables in hidden input inside form

<input type="hidden" name="someVariable" value="someValue">

and remove $(document).on("submit",... event

You can try

var input = $("<input>").attr("type", "hidden").attr("name", "someVariable").val("someValue");
$('#formUpload').append($(input));

this links may help you

http://www.malsup.com/jquery/form/progress2.html

http://www.malsup.com/jquery/form/file-echo2.php.txt

Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
0

Well, in case your form is being added dynamically then you'd have to use DOMNodeInserted event instead of submit. That way, whenever there's some addition in DOM your form will be attached to form plugin.

You can replace your function with following --

    $(document).on("DOMNodeInserted", "form#formUpload", function() {
        $(this).ajaxForm({
            data: { someVariable : 'someValue' },
            complete: function(xhr) {
                // do something 
            }
        });
    });

But remember, using DOMNodeInserted event will fire that method whenever there's addition of any kind into DOM. So just put what is essential ( in this case form plugin init for #formUpload ) .

SachinGutte
  • 6,947
  • 5
  • 35
  • 58
0

Try to locate if you already added the jQuery Form Plugin...

<script src="jquery.form.js"></script>

Your syntax is definitely correct according to http://malsup.com/jquery/form/#options-object

nickb
  • 59,313
  • 13
  • 108
  • 143
jrran90
  • 668
  • 12
  • 18