views:

40

answers:

2

I am having problems using the jquery validation plugin's submitHandler.

I am working within the confines of some code that I do not have access to change so I am trying to find some work arounds.

This is what I would like to do... After the form validates, when the user clicks the submit button, and it is successful redirect the page to a different page.

If I were not using jquery, I'd use the location.replace method and that is what I tried to use, but it does not seem like the submitHandler is executing because I tried a simple alert('TEST'); and it did not work.

$(document).ready(function(){
  $("#myForm").validate({
    submitHandler: function(form){
      location.replace('http://www.google.com');
      form.submit();
      }
   });
});

I tried this to test it out but it also did not work...

$(document).ready(function(){
  $("#myForm").validate({
    submitHandler: function(form){
      alert("TEST");
      form.submit();
      }
   });
});

The form submits and sends the email as it is supposed to when all of the fields are valid and it does not send the email with the fields are not valid. The only problem I have is handling the relocation of the page.

Any help anyone can provide would be greatly appreciated!

A: 

form.submit is a function, so you need to call it, like this: form.submit().

However, if you change the location of the page before the form is submitted (which is what location.replace() does!) means the form won't be properly submitted. Are you trying to redirect the user after the form has been submitted? If so, you'll have to send the redirect from the server, or (as @Amir said in the comment below) use Ajax to submit the form, and do your location.replace() call in the ajaxComplete handler.

As far as I know, once you've submitted the form - unless you use Ajax to submit it - you're really not in control of the page anymore, since it's on its way out. I haven't tested it myself, though.

Matt Ball
Or do ajax post and oncomplete do redirect. :)
Amir Raminfar
@Amir: yup, I just thought of that.
Matt Ball
ok, I just reviewed my code and I do have the form.submit() correct. I didn't copy it right when I put it here.
Dave L.
What I was doing before was setting a timeout of 4000 (4 seconds) and then redirecting the page. I wish I had the option to use Ajax, but I don't have complete control of the code.
Dave L.
@user: I updated your question to show `form.submit()`. If you can't use Ajax then you're almost certainly going to have to redirect from the server. When a (non-Ajax) submit happens, the browser sends an HTTP request (usually a `GET` or `POST`) to the server, and after that, it does whatever the server tells it.
Matt Ball
essentially, I was doing this... setTimeout("location.replace('http://www.google.com')", 4000);
Dave L.
@user: Every time you pass a string to `setTimeout`, a ninja cuts off a puppy's head. `:(` Do it like this: `setTimeout(function(){ location.replace('http://www.google.com');}, 4000);` Just an FYI.
Matt Ball
@Matt Thanks for your help, Matt, however it is not working. I tried to code in an alert to see if that would work, but it is not working either with or without the timeout.
Dave L.
@user: why don't you edit your question to include a full snippet of the code that you say "isn't working?" What do you mean by "isn't working?" Is the form not submitting? Is the `location` not changing? Is it both?
Matt Ball
@Matt I updated the question and to answer your question the location is not changing and I can't trigger the alert('Test') in the place of the location change in the submitHandler.
Dave L.
+1  A: 

Your syntax isn't correct:

("$myForm").validate({

Should probably be:

$("#myForm").validate({

Or if it's a class:

$(".myForm").validate({

When debugging what isn't working, always check the console for errors first, what you have currently is certainly throwing a syntax error.

Nick Craver
+1. Totally missed that. It's definitely time to get off SO for the day
Matt Ball
@Nick - Thanks Nick, I had the syntax correct, I typed it correctly on here.
Dave L.
@Dave - your corrected question still isn't right, it's looking for a `<myForm>` element, not something with that ID or class, what does your `<form>` tag look like?
Nick Craver
<form id="myForm" name="myForm" class="validationForm" action="/myaction">
Dave L.
@Dave - with your corrected syntax are you seeing the alert, or any other JavaScript errors?
Nick Craver
@Nick - I am not =(
Dave L.