PHP中表单验证提交不了,跳转但不验证

PHP中表单提交不上,跳转但不验证

img

开发环境:
宝塔·腾讯云专享版
MySQL:5.5.62

问题描述:
PHP提交不上,直接跳转到 login.php页面但是不执行表单验证,数据库连接的密码,路径都没问题
HTML代码:

<form method="post" action="login.php" id="loginFrom" onsubmit="return func()">
     <div class="form-group">
     <input id="username" name="username" required class="form-control w-100 text-center" placeholder="Username …" type="username">
     </div>
     <div class="form-group">
     <input id="password" name="psaaword" required class="form-control w-100 text-center" placeholder="Password …" type="password">
     </div>
     <div class="form-group">
     <button id="login" class="btn btn-default btn-full" type="submit" onclick="check(this)">登录</button>
     </div>
</form>

php文件:

  1. login.php:
<?php
header('content-type:text/html;charset=utf-8');
//登录界面
require 'login_db_connect.php';//连接数据库


//判断表单是否提交,用户名密码是否提交
if (isset($_POST['username'])&&isset($_POST['password'])){//登录表单已提交
   //获取用户输入的用户名密码
   $username=$_POST["username"];
   $pwd=$_POST["password"];
   $sql="select username,password from user where username='$username' and password='$pwd';";
   $result=mysqli_query($con, $sql);//执行sql语句
   $row=mysqli_num_rows($result);//返回值条目
   if (!$row){//若返回条目不存在则证明该账号不存在或者密码输入错误
       echo "<script>alert('该账号不存在,请前往注册');location='register.php'</script>";
       //exit('账号或密码错误');
   }else{//存在返回条目证明用户账号密码匹配,进入主页面
       $_SESSION['username']=$_POST['username'];
       echo "<script>alert('风雨潇潇待你归,欢迎登录');location='hello.html'</script>";
   }   
}


require 'index.html';

2.数据库连接php

<?php
//用于登录界面数据库连接
//设置字符集
header('Content-type:text/html;charset=utf8');

//连接数据库
$con=mysqli_connect("localhost","root","fiijAjeqIBiRH6","hellosweet_love");
if (mysqli_connect_errno($con))
{
   echo "连接 MySQL 失败: " . mysqli_connect_error();
} 

第一个问题,表单的action先于click事件触发,所以会导致还没触发检验就会跳转到action中的路径。
第二个问题,name="psaaword"应该是name="password"
第三个问题,login.php里面也有一处写成了psaaword

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^