How to go from one page to another page using javascript?

后端 未结 9 528
野性不改
野性不改 2021-01-30 19:37

From an admin page if the user is valid then the browser should go to another page.

When a user enters his user name and password, then clicks the OK button then another

9条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 20:06

    Easier method is window.location.href = "http://example.com/new_url";

    But what if you want to check if username and password whether empty or not using JavaScript and send it to the php to check whether user in the database. You can do this easily following this code.

    html form -

    javascript validation-

    function validateForm(){
        var uname = document.forms["myForm"]["username"].value;
        var pass = document.forms["myForm"]["password"].value;
    
        if((!isEmpty(uname, "Log In")) && (!isEmpty(pass, "Password"))){
    
            return true;
        }else{
            return false;
        }
    }
    
    function isEmpty(elemValue, field){
        if((elemValue == "") || (elemValue == null)){
            alert("you can not have "+field+" field empty");
            return true;
        }else{
            return false;
        }
    }
    

    check if user in the database using php

    ";
        }
    
        $uname_tb = $_POST['username'];
        $pass_tb = $_POST['password'];
    
        $query ="SELECT * FROM user";
        $result = mysqli_query($con,$query);
    
        while($row = mysqli_fetch_array($result)){
            if(($row['username'] == $uname_tb) && ($row['password'] == $pass_tb)){
                echo "Login Successful";
                header('Location: dashbord.php');
                exit();
            }else{
                echo "You are not in our database".mysqli_connect_error();
            }
        }
        mysqli_close($con);
    ?>
    

提交回复
热议问题