0

I want to perform an ajax request right before my form is submitted, and then trigger the form submission from my ajax callback. But when I try to trigger the submit, I get the following error:

Uncaught TypeError: Property 'submit' of object # is not a function

Here is the entire code:

    <form id="myform" method="post" action="http://www.google.com"  >
        <input type="text" name="email" id="test" size="20"  />
        <input id="submit" name="submit" type="submit" value="Submit"  />
    </form>

    <script>

    function do_ajax() {

        var jqxhr = $.get("#", function(){

            // Once the ajax function is complete, I want to submit my form:
            $('#myform').submit();

        });
    }

    $(function() {

        $('#submit').click(function (e) {


            // When the user clicks submit, I want to perform an ajax request first:
            e.preventDefault();
            do_ajax();

        });
    });

</script>

Stumped.

1
  • have you tried logging $("#submit") to console to see if it actually contains the form? Commented Mar 20, 2012 at 0:06

3 Answers 3

2

I think the problem is that the submit button is interfering by its name. I have read something about this, will Google a link.

Just change the name and ID and it will work:

    <input id="submit2" name="submit2" type="submit" value="Submit"  />
Sign up to request clarification or add additional context in comments.

2 Comments

Hm, you don't get rid off the error? I got exactly the same error as you in this version: jsfiddle.net/vW322/1 but when i changed the name of the submit button it works jsfiddle.net/vW322/2 (But you cannot post to Google inside jsfiddle so it will apper another message)
SON OF A BITCH- that's 4 hours of life I won't get back. Thank you Simon you're a gentleman and a scholar!
1

From the example I don't have really clear what you want to do anyway to handle the submit event:

$("#formId").submit(function() {
  // Do here whatever you want
  return false;
});

And to fire the submit with click on another object (not the submit button).

$("#another").click(function() {
  $('#formId').submit();
});

I suggest you give an ID to your form (a page can have multiple forms) and that you check your line with get("#"). What should it do?

1 Comment

Adriano- these suggestions don't solve the problem. My code is simplified for example's sake- See my edits I've commented the code to make the purpose clear: Perform an AJAX function before the form is submitted.
0

This should work as your calling the function inside your ajax callback, not tested and may not be the most elegant solution but here goes...

(function($){

    $(function(){

        var evtListner = function(){
            $("#submit").on('click', function(e){
                e.preventDefault();
                do_ajax($(this));
            });
        }

        var do_ajax = function(scope){
             var scope = scope;
             $.get('#', function(){
                 submit_it(scope);
             });
        }

        var submit_it = function(scope){
            scope.submit(function(){
                //
            });
        }

        evtListner();//initial call
    });

})(jQuery);

1 Comment

Thanks, but my brain can only process 3 levels of nested functions. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.