如何使用 JavaScript PHP 和 MariaDb 验证登录表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16953953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:35:30  来源:igfitidea点击:

How to validate login form using JavaScript PHP and MariaDb?

phpjavascriptmariadb

提问by ladybug

I am starting to learn JavaScript today. I want to validate the user login form using JavaScript and MariaDB. I know how to connect to database using PHP, but if I have to do it through JavaScript, is that possible? I

我今天开始学习 JavaScript。我想使用 JavaScript 和 MariaDB 验证用户登录表单。我知道如何使用 PHP 连接到数据库,但是如果我必须通过 JavaScript 来连接,这可能吗?一世

I want to get the user's table using JavaScript. The user will get an error when, of course, their username/password does not match what they have on the database. I am really curious about this.

我想使用 JavaScript 获取用户的表。当然,当用户的用户名/密码与数据库中的用户名/密码不匹配时,用户会收到错误消息。我真的很好奇这个。

<form name="loginForm" id="loginForm" method="post" action="login.php" onsubmit="return validateLoginForm();">
    Username: <input type="text" name="username">
    Password: <input type="password" name="password"> <br/>
    <input type="submit" name="login" value="Login"/>
</form>

If the user click the button, will connect to the table using JavaScript. But I dont know how to do it.

如果用户单击按钮,将使用 JavaScript 连接到表。但我不知道该怎么做。

Thanks.

谢谢。

回答by MaxI

I am going to use jquery which is an excellent javascript framework. Since you have no code to show, I will only post for you pseudo code to show you how you can validate a username and password using jquery,php and mariadb. The mysqli API in php will work with mariadb HTML\Client side

我将使用 jquery,它是一个出色的 javascript 框架。由于您没有代码可显示,我只会为您发布伪代码,向您展示如何使用 jquery、php 和 mariadb 验证用户名和密码。php 中的 mysqli API 将与 mariadb HTML\Client 端一起使用

<html>
<head>
<script src="jquery-1.X"></script>
<script>
$(document).ready(function(e){
var username=$("#username").val();
var password=$("#password").val();
$("#login").click(function(){
if(username=="" && password=="")
{
alert("username or password is blank");
e.preventDefault();//prevent the form from submitting.
}

})

})
</script>
</head>
<body>
<form name="loginForm" id="loginForm" method="post" action="login.php">
    Username: <input type="text" name="username" id="username">
    Password: <input type="password" name="password" id="password"> <br/>
    <input type="submit" name="login" id="login" value="Login"/>
</form>
</body>
</html>

PHP Side - Login.php

PHP 端 - Login.php

<?php 
include("conn.php");
$username=$_POST['username'];
$password=sha1($_POST['password']); //my passwords are hashed in the database using the sha1

$checklogin="SELECT * FROM users_tbl WHERE Username=? AND Password=?";
$query = $connection->prepare($checklogin);
$query->bind_param("ss",$username,$password);
$query->execute() or die($connection->error);
$count = $query->num_rows;
if($count==1)
   {
    while($row=$query->fetch_assoc)
        {
     $_SESSION['username']=$row['Username'];
    }
header("Location:index.php")
   }
 ?>

PHP Connection File - conn.php

PHP 连接文件 - conn.php

<?php
//connect to database 
$connection = new mysqli("localhost","user","password","mydatabase"); 
if(mysqli_connect_errno()){
    printf("Connection failed: %s\n",mysqli_connect_error());   
    exit(); 
}
?> 

Please not this is a very basic login script to give you a general understanding. You need to read up on jquery and PHP in detail.

请不要这是一个非常基本的登录脚本,可以让您大致了解。您需要详细阅读 jquery 和 PHP。