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";
        })
    });
}
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
user3147424
  • 3,022
  • 5
  • 19
  • 22
  • 1
    possible duplicate of [How do I use jQuery to redirect?](http://stackoverflow.com/questions/14591428/how-do-i-use-jquery-to-redirect) – Ram Jan 26 '14 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/? – user3147424 Jan 26 '14 at 17:59

3 Answers3

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;        
};
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • @user3147424, Do you really need form. Just try to use without form. And Please remove your comment so that we can continue interacting – Satpal Jan 26 '14 at 18:36
1

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

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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" ...
pathfinder
  • 1,606
  • 18
  • 22