-1

我的页面中有一个登录表单,我想测试用户是否填写了所有输入,这是我尝试过的代码,但它不起作用:

<?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>

问题是当我单击提交按钮时,尽管输入未填写,它仍将我重定向到操作页面

4

2 回答 2

0

问题是浏览器会type="submit"在 JavaScript 脚本完成执行之前提交一个按钮。您想要做的是让您的脚本在验证后提交您的表单。因此,将您的输入类型更改为按钮,并从 javascript 本身进行提交。

编辑:(每条评论)

您已经通过 JavaScript 提交表单。为了让您的代码正常工作,我只需将您的输入按钮从提交类型更改为按钮。所以改成<input type="submit" value="Valider" name="valider" id="valider">这样:

<input type="button" value="Valider" name="valider" id="valider">
于 2013-02-21T17:02:35.750 回答
0

你可以试试

<?php 
session_start();
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');
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <script type="text/javascript">
            function validation(form)
            {
                var login = document.getElementById('login');
                var mdp = document.getElementById('mdp');
                if(login.value == '' || mdp.value == '')
                    alert('You must fill in all fields!');
                else
                    form.submit();

                return false;
            }
        </script>
    </head>
    <body>

        <form method="post" action="authentification.php" id="form1" name="form1" onsubmit="validation(this);return false">
            <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>
于 2013-02-21T17:10:56.983 回答