1

I'm submitting a form with jquery and can't seem to stop the page from reloading after the form submits.

My current code looks like this:

HTML:

<form class="form-horizontal" method="post" action="#" name="basic_validate" id="basic_validate" />
  <div class="control-group">
    <label class="control-label">Image Path</label>
    <div class="controls">
      <input type="text" name="imagepath" id=imagepath />
    </div>
  </div>
  <div class="form-actions">
    <input type=button value="Send" id="sendemailbtn" class="btn btn-primary" />
  </div>
</form>

jQuery:

$("#sendemailbtn").click(function(e) {
    e.preventDefault();
    $("#basic_validate").submit();

    if ($("#basic_validate").children('.control-group').hasClass('error')) {
        return false;
    } else {
        $.post('send_email.php', $("#basic_validate").serialize(), function(data) {

            // I see output on the server side but never hit this area after the submission,

            console.log(data);
        }, "json");
    }
});        

3 Answers 3

3
$("#basic_validate").submit();

Is your culprit. That line submits the form and makes the page reload.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there anyway to prevent the page load?
If you omit that line (remove/comment out) and let your script continue. Try it out.
that won't work because I do some form validation on a form submisison
Do you need to submit the form to validate it? You have already catched the click event on #sendemailbtn.
1

According to the submit doc,

[...] We can cancel the submit action by calling .preventDefault() on the event object or by returning false from our handler.

As .preventDefault() doesn't seem to work for you, try :

$("#basic_validate").submit(function(){

    if ($("#basic_validate").children('.control-group').hasClass('error')) {
        return false;
    }
    else {
        $.post('send_email.php', $("#basic_validate").serialize(), function(data) {

        // I see output on the server side but never hit this area after the submission,

            console.log(data);
        }, "json");                             
    }            
    return false;  //This prevents reloading
});         

Comments

0

That's because .submit() works that way. If you want to submit the form with AJAX then you want to make an AJAX request manually with $.post without .submit():

$("#sendemailbtn").click(function (e) {
    e.preventDefault();
    if (!$("#basic_validate").children('.control-group').hasClass('error')) {
        $.post('send_email.php', $("#basic_validate").serialize(),
        function(data) {
            console.log(data);
        }, "json");                             
    }    
});        

Comments

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.