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 -
<form name="myForm" onsubmit="return validateForm()" method="POST" action="login.php" >
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="submit" name="submitBt"value="LogIn" />
</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
<?php
$con = mysqli_connect("localhost","root","1234","users");
if(mysqli_connect_errno()){
echo "Couldn't connect ".mysqli_connect_error();
}else{
//echo "connection successful <br />";
}
$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);
?>