我的页面中有一个登录表单,我想测试用户是否填写了所有输入,这是我尝试过的代码,但它不起作用:
<?php session_start() ?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var check {};
check['login'] = function () {
var login = document.getElementById('login');
if(login.value.length > 0)
{
return true;
}
else
{
return false;
}
};
check['mdp'] = function () {
var mdp = document.getElementById('mdp');
if(login.value.length > 0)
{
return true;
}
else
{
return false;
}
};
var inputs = document.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++)
{
inputs[i].onkeyup = function() {
check[this.id](this.id);
}
}
var result = true;
for (var i in check)
{
result = check[i](i) && result;
}
var myForm = document.getElementById('form1');
if (!result)
{
alert('Vueillez saisir tous les champs !');
}
myForm.addEventListener('valider', function (e) {
if (!result) {
e.preventDefault();
}
});
</script>
</head>
<body>
<?php
require_once('bdd.inc.php');
if (isset($_POST['valider'])) {
$auth = false;
foreach($clients as $client)
{
if($_POST['login'] == $client['Pseudo'] && $_POST['mdp'] == $client['MotDePasse'])
{
$_SESSION['login'] = $_POST['login'];
$_SESSION['mdp'] = $_POST['mdp'];
$auth = true;
break;
}
}
if(!$auth)
{
header('location:authentification.php?auth=false');
}
}
?>
<form method="post" action="authentification.php" id="form1" name="form1">
<table>
<tr>
<td>Login :</td>
<td><input type="text" name="login" id="login"></td>
</tr>
<tr>
<td>Mot de passe :</td>
<td><input type="text" name="mdp" id="mdp"></td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value="Valider" name="valider" id="valider"></td></tr>
<?php
if(isset($_GET['auth']))
{
if($_GET['auth'] == 'false')
{
echo '<div style="color:red">Désolé, Votre pseudo ou votre mot de passe est erroné !</div>';
}
}
?>
</table>
</form>
</body>
</html>
问题是当我单击提交按钮时,尽管输入未填写,它仍将我重定向到操作页面