1

I have worked for hours on this with little joy. I am a total beginner on jQuery. On submitting a form I want to stop the form processing, load an external file, then process the form. Everything works apart from the final line of submitting the form. Can someone point me in the right direction.

<script type="text/javascript">
    $("#form1").on('submit', function(e) {
        e.preventDefault();
        $("#a").load("saveData.php");
        $('#form1').submit();
    });
</script>
1
  • What does that external file do? Submission of form depends on it? Commented Jul 2, 2013 at 5:53

3 Answers 3

1

You need

$("#form1").on('submit', function (e) {
    e.preventDefault();
    var form = this;
    $("#a").load("saveData.php", function(){
        form.submit(); // we need to use a non jQuery form reference here to prevent recursive submit calls
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
$("#form1").on('submit', function (e) {
    e.preventDefault();
    var $this = this;
    $("#a").load("saveData.php", function(){
        $this.submit(); //assuming you want to submit the form after you have loaded the file
    });
});

Comments

0

You've got an infinite loop, because the submitting the form invokes your submit handler again.

Just leave out e.preventDefault();. When the handler returns, normal form submission will happen.

2 Comments

If I take out e.preventDefault(); the form submits ok but does not load the external file.
When you submit a form, the page is replaced with whatever the server returns. If you don't want to do that, you have to use AJAX instead of normal form submission.

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.