views:

93

answers:

2

I'm using jquery with the jquery form plugin. I read through a bunch of posts, the docs, etc., and I'm still having trouble with what I believe to be simple. I have a form with some simple text inputs and a couple textareas, and I have this link that I want to trigger submission of that form to a specific method (/email):

<a id="email-data">Send Email</a>

and this jquery in doc ready:

$('#email-data').click(function() {
    var options = {
        url: '/email/',
        success: alert('Email sent.')
    };
    $('#report-giftcard-sales-form').ajaxSubmit(options);   
});

So I would expect it to submit to my /email method, but no such luck. Currently I just have the /email method logging a debug message, so it's as simple as can be.

Any help is greatly appreciated.

A: 

Callback from success option can't be alert because it require more than one param at the callback function. You need to write something like this:

$('#email-data').click(function() {
    var options = {
        url: '/email/',
        success: formSended
    };
    $('#report-giftcard-sales-form').ajaxSubmit(options);   
});
function formSended(responseText, statusText, xhr, $form) {
  alert('Email sent.Status: '+statusText);
}
antyrat
@antyrat: yes, i've tried that way as well. I don't get the alert, and the method doesn't post. I know it's not a syntax error because other js on page is fine.
k00k
when I add the +statusText to the alert message, it is 'undefined'
k00k
You add statusText inside callback function to the alert or at the options to the alert? Do you have live example?
antyrat
I add statusText inside callback function by copying your example. I don't have live, sorry :(It's definitely not hitting the /email method.
k00k
Do you use firebug or other JS debugger?
antyrat
Yes, I now see: "Uncaught ReferenceError: jQuery is not defined - jquery.form.js:11"
k00k
I also see: "Uncaught TypeError: Object #<an Object> has no method 'ajaxSubmit' - site.js:14"
k00k
+1  A: 

Answering my own question here...

Hey stupid, yes you (a.k.a. me), you need to load jquery BEFORE you load a jquery plugin!

(I of course know this. Sometimes stupidity just happens when you're working too fast)

Good Example:

<script type="text/javascript" src="/js/jquery.min.js"></script> 
<script type="text/javascript" src="/js/jquery.form.js"></script>
k00k
Don't forget to accept your answer ;)
antyrat