5

I'm learning Ajax form process. My problem is: When I submit a form it works and redirects to another page. But when I click the back button there, I don't get redirected to the previous page. What am I doing wrong here?

 <form  id="form-admin-login">
   <input name="username" type="text" placeholder="User name" class="input-field" required="">
   <input name="password" type="password" placeholder="Password" class="input-field" required="">
     </form>
   <button name="submit" onClick="submit_login()" class="btn btn-login">Login</button>

ajax

function submit_login(){
    $.ajax({
        type: "POST",
       data: $("#form-admin-login").serialize(),
        url: "ajax/ajax-admin-login.php",

        dataType: "json",
        cache: false,
        beforeSend:function(){

        },

        success: function(data) {

            if(data['error']==0)
            {
                window.location.replace('dashboard.php');

            }
            else if (data['error']==1)
            {

                $("#showError").removeClass("hide");
               $("#error-message").html(data['message']);

            }
        },

        error: function(jqXHR, textStatus, errorThrown){

        }
    })
2
  • 1
    You need to post the relevant code. Also, when you use ajax, your form submit should not result in a redirect (normally...). Commented May 17, 2015 at 8:15
  • ok. i'll submit the code wait Commented May 17, 2015 at 8:17

1 Answer 1

6

You need to redirect with

window.location.assign(url);
window.location = url;
window.location.href = url;

not with

window.location.replace(URL);

because the location.href will simply navigate to the new URL. The replace method on the other hand navigates to the URL without adding a new record to the history.

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

2 Comments

Mostly happens with us.... Simple mistakes make us to think for day and night....
thats true it was a silly mistake .. have to build a tajmahal for stackoverflow :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.