1

I am new to javascript and jquery and need your help out. I have tried searching the forums but I was unable to find a solution

i am trying to post something to a php file and then redirect to a page of my choice. The post is happening properly but not the redirect.

The html form is

<form action="userinfo.php" method="post">
    <p><input type="text" required maxlength=7 name="login" id="login" value="" placeholder="Username"></p>
    <p><input type="password" required name="password" id="password" value="" placeholder="Password"></p>
    <p class="remember_me">
      <label>   
        <input type="checkbox" name="remember_me" id="remember_me">
        Remember me on this computer
      </label>
    </p>
    <p class="submit"><input type="button" id="searchForm" onclick="SubmitForm();redirect()" value="Login"></p>
  </form>

and the jquery code is

<script>
        function SubmitForm() {
        var login = $("#login").val();
        var password = $("#password").val();

        $.post("userinfo.php", {login: login, password: password },
        function(data) {
            alert("Data Loaded: " + data);
        });
        }
        function redirect(){
            alert("Redirecting..");
            window.location = "mainpage.html"
        }
    </script>

SO redirect() is obviously not being called.Any help as to why this is happening is appreciated. Please note that i am not a javascript or jquery expert. Just trying my luck to learn something.. :-)

p.s: I can redirect from the php file but wouldn't prefer to do that.

0

3 Answers 3

2

you need to redirect only after the form submission(login) is successfully completed

<input type="button" id="searchForm" onclick="SubmitForm()" value="Login">

then

<script>
    function SubmitForm() {
    var login = $("#login").val();
    var password = $("#password").val();

    $.post("userinfo.php", {login: login, password: password },
    function(data) {
        alert("Data Loaded: " + data);
        redirect();//redirect here
    });
    }
    function redirect(){
        alert("Redirecting..");
        window.location = "mainpage.html"
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You have to call the function when the post request got succeeded

    <script>
                function SubmitForm() {
                var login = $("#login").val();
                var password = $("#password").val();

                $.post("userinfo.php", {login: login, password: password },
                function(data) {
                    alert("Data Loaded: " + data);
                    // calling the redirect
                    redirect();
                });
                }
                function redirect(){
                    alert("Redirecting..");
                    window.location = "mainpage.html"
                }
        </script>

Comments

0

First of all why are you using both form action and jquery post for same operation. Thats not a good practice.

Use jquery post data to call php and if the response id success the redirect to next page.

 <script>
    var login = $("#login").val();
    var password = $("#password").val();

    $.post("userinfo.php", {login: login, password: password },
    function(data) {
        if(data = "success"){
         console.log("Redirecting..");
         window.location = "mainpage.html"
        }
    });
    }
</script>

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.