views:

19

answers:

1

I've been trying to figure out how to work this jQuery Form plugin to upload a file, but it doesn't seen to do what I need it to do.

I have the jQuery:

$('.upload-file').click(function()
{   
    $('#htmlForm').ajaxForm({ 
        // target identifies the element(s) to update with the server response 
        target: '#htmlExampleTarget', 

        // success identifies the function to invoke when the server response 
        // has been received; here we apply a fade-in effect to the new content 
        success: function(msg) { 
            $('#htmlExampleTarget').hide().html(msg).fadeIn('slow'); 
        } 
    }); 
});

The HTML:

<div id="container">
    <form action="upload.php" method="post" id="htmlForm" enctype="multipart/form-data">
        <p>E-mail<br />
        <input type="text" name="email" id="email" /></p>
        <p>File<br />
        <input type="file" name="upload" id="upload" /></p>
        <p><input type="button" value="Submit" class="upload-file" /></p>
    </form>
</div>
<div id="htmlExampleTarget"></div>

The jQuery Form plugin is included in the <script> in the <head></head> with the $(document).ready(function() whenever I click on the button to submit the form, it doesn't do anything. Although, if I change it to

<input type="submit" value="Submit" class="upload-file" />

The form submits, but goes to upload.php when I should expect it to output the HTML in the <div id="htmlExampleTarget"></div> and it doesn't do that.

EDIT:

<script type="text/javascript" src="js/jquery.form.min.js"></script>
<script type="text/javascript" src="js/jquery.1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.wmk.min.js"></script>
<script type="text/javascript" src="js/uimg-core.js"></script>
+1  A: 

.ajaxForm() is for setting up a <form>, not submitting it, instead you want to run it on document.ready:

$(function() {
  $('#htmlForm').ajaxForm({ 
    target: '#htmlExampleTarget', 
    success: function(msg) { 
        $('#htmlExampleTarget').hide().html(msg).fadeIn('slow'); 
    } 
  });
});

Then do change your type to be a submit button:

<input type="submit" value="Submit" class="upload-file" />

The alternative (though this doesn't degrade gracefully) is to call .ajaxSubmit() like this:

$('.upload-file').click(function() {   
  $('#htmlForm').ajaxSubmit({ 
    target: '#htmlExampleTarget', 
    success: function(msg) { 
        $('#htmlExampleTarget').hide().html(msg).fadeIn('slow'); 
    }
  });
});

This actually submits the form right now, rather than setting it up to send via AJAX on it's submit handler.

Nick Craver
What would the HTML be?
YouBook
@YouBook - In the second example the html wouldn't change it could still be anything...if it's a `type="submit"` button you need to `event.preventDefault()` or `return false` though.
Nick Craver
It keeps bugging me, always going to `upload.php`...
YouBook
@YouBook -Sounds like you have a JavaScript error and the default behavior is kicking in, are there any errors in your console?
Nick Craver
Just used Firebug with jQuery debugger. It says: `jQuery is not defined [Break on this error] else if(window.opera}}};})(jQuery);` on jquery.min.js (line 83)
YouBook
@YouBook - Can you update the question with your `<script>` includes?
Nick Craver
Updated the post
YouBook
@YouBook - Swap the 1st and 2nd, you need to include jQuery **before** any plugins that rely on it :)
Nick Craver
WOAH :O That totally fixed my problem, I never knew the order of including scripts affected each plugin, thanks! :D I wish I could rep you +10,000 ;)
YouBook