0

I have a web form to let user enter some information and submit. I want such behavior that if the submission is successful, the it will go to a page. But if there are some form submission errors (in my case is due to existing ID), I want the form stay there without going to the next page and alert the user "ID already exists". I used jQuery to handle the success and fail events:

function checkForm() {

$.post("/myapp/rest/customer/created", $("form").serialize(), function(
    data, status) { })
.done(function() {alert("post success");})
.fail(function() {alert("post error");});
return false;
}

My form is:

<form action="/myapp/rest/customer/created"
    onsubmit="return checkForm();" method="POST">
    <table border="1">
        <tr>
            <td>Customer name:</td>
            <td><input type="text" id="name" name="name"></td>
        </tr>
        <tr>
            <td>Customer ID:</td>
            <td><input type="text" id="id" name="id"></td>
        </tr>
        <tr>
            <td>Customer DOB:</td>
            <td><input type="text" id="dob" name="dob"></td>
        </tr>
    </table>
    <br /> <input type="submit" value="Submit">
</form>

After submission, no matter whether the submission is a success or failure, I only see the popup message box saying "post success" or "post error". But it will never go to the next page if it's successful. I even tried a different approach like the code below but it didn't work either:

function checkForm() {
    $.post("/myapp/rest/customer/created", function(data, status) {
      if (status === "success") {
        alert("post success");
      } else {
        alert("post error");
      }
    });

    return false;
}

So how to correct it to make it redirect to the next page (/myapp/rest/customer/created) if the submission is successful?

5
  • You're always returning false. Commented Jun 18, 2013 at 15:06
  • @j08691: I can't return true/false based on the status because it will always direct to the next page if I do so. Commented Jun 18, 2013 at 15:06
  • The return false is just fine. You just need to redirect the user with window.location.href... Commented Jun 18, 2013 at 15:07
  • @tonga - why not? if you return false you cancel the submit. Commented Jun 18, 2013 at 15:08
  • @j08691: It won't block in case of error if you use that way. Commented Jun 18, 2013 at 15:13

1 Answer 1

1

You can redirect within your success conditional using window.location.href:

if (status === "success") {
  window.location.href = 'http://url.com'
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Matthew. I used this approach and it works. But...after the submission is successful, it shows an error page: HTTP Status 405 - Method Not Allowed. What causes that problem?
That means whatever URL you are redirecting to doesn't allow GET.
I used "POST" method for the submission. Previously I saw the correct page if the submission is correct.
The redirect method and the form method are separate. Can you manually go to the URL without the error?
What do you mean by "manually go to the URL"? In the jQuery post, the first argument is the URL I want to post, right? So in case of success, should it redirect to that URL specified in the $.post()?
|

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.