1

I have an ajax call and am trying to redirect my page after it makes the call to another URL. However, it just refreshes the page. What can I do to redirect my page?

<button onclick="SignUp()" class="btn btn-success text-center">Submit Information</button>

function SignUp() {

    var first_name = document.getElementById('first_name').value;
    var last_name = document.getElementById('last_name').value;

    $.ajax({
        type: 'POST',
        url: 'signup.php',
        data: { "first_name": first_name, "last_name": last_name },
        async: false,
        success: (function () {
            window.location.href = "http://stackoverflow.com";
        })
    });
}
2
  • 1
    possible duplicate of How do I use jQuery to redirect? Commented Jan 26, 2014 at 15:09
  • i've tried that post already, does not work, still just acts like a refresh. after the ajax call, the url became www.example.com/signup/? Commented Jan 26, 2014 at 17:59

3 Answers 3

2

Use

  window.location.href = "http://stackoverflow.com";

EDIT

As button is in form its default behavior is submit

So you have to add type="button" to button like

<button type="button" >Submit Information</button>

OR

You need to use return false

HTML

<button onclick="return SignUp()" >Submit Information</button>

JavaScript

function SignUp() {
    //Your code  
        return false;        
};
Sign up to request clarification or add additional context in comments.

1 Comment

@user3147424, Do you really need form. Just try to use without form. And Please remove your comment so that we can continue interacting
1

Your submit button is submitting the form before the success handler is processed.

Add type="button" so it isn't a submit button.

2 Comments

type="button" ended up being the final piece to the puzzle, thanks!
actually, it still does not redirect when there is data in the form. . .
0

Since your button is in a form you need to add this to onClick or the page refreshes due to a form submit.

<button onclick="SignUp()"; return false" ...

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.