【问题标题】:Form submission takes twice attempts to send info to DB表单提交需要两次尝试将信息发送到数据库
【发布时间】:2019-10-21 10:37:05
【问题描述】:

我是 PHP 新手,一直在为我的大师开发登录系统,但遇到以下问题。 我创建了一个类系统来操作我的代码并在不同的需求中重用它,然后我创建了表单(注册、登录、更改密码、更新......),问题是任何形式,当我尝试提交信息,它不会在第一次提交,而是在第二次提交,所有工作都很好(验证和创建用户,登录等......)。 我的表单中的操作为空,我尝试将操作设置为同一页面但结果相同。 我知道这可能是因为第一次尝试时 url 是空的,需要第二次才能继续,但我不知道该怎么做。 这是我的注册表单的代码:

<!-- -------------------- REGISTER.PHP -------------------- -->

<?php

require_once 'core/init.php'; // Check name of the initialization file

include 'includes/heade&header.php';      // HEAD and HEADER of the site

if(Input::exists()) {
    if(Token::check(Input::get('token'))) {

        $validate = new Validate();
        $validation = $validate -> check($_POST, array(
            'username' => array(            // Check name of input
                'required' => true,         // Add conditions to the inputs
                'min' => 2,
                'max' => 20,
                'unique' => 'users'
            ),
            'firstname' => array(                // Check name of input
                'required' => false,         // Add conditions to the inputs
                'min' => 2,
                'max' => 50,
                'text' => true
            ),
            'lastname' => array(        // Check name of input
                'required' => false,        // Add conditions to the inputs
                'min' => 2,
                'max' => 50,
                'text' => true
            ),
            'email' => array(       // Check name of input
                'required' => true,     // Add conditions to the inputs
                'email' => true
            ),
            'password' => array(            // Check name of input
                'required' => true,         // Add conditions to the inputs
                'min' => 6,
                'max' => 12,
                'password_NUM' => true,
                'password_CAP' => true,
                'password_LOW' => true,
                'password_S-CHAR' => true
            ),
            'password_again' => array(      // Check name of input
                'required' => true,         // Add conditions to the inputs
                'matches' => 'password'
            )

            // If there are more inputs in the form extend to needs

        ));

        if($validation -> passed()) {

            $user = new User();

            $salt = Hash::salt(32);

            try {
                $user -> create(array(
                    'username' => Input::get('username'),
                    'firstname' => Input::get('firstname'),
                    'lastname' => Input::get('lastname'),
                    'email' => Input::get('email'),
                    'password' => Hash::make(Input::get('password'), $salt),
                    'salt' => $salt,
                    'joined' => date('Y-m-d H:i:s'),
                    'group' => 1
                ));

                Session::flash('home', 'You have been registered and can now log in!');
                Redirect::to('index.php');

                // Add email of confirmation to complete the app

            } catch(Exception $e) {
                die($e -> getMessage());
            }
        } else {
            foreach($validation -> errors() as $error) {
                echo $error, '<br/>';
            }
        }
    }
}

?>
<form action="" method="post">

    <div class="field">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')); ?>" autocomplete="off">
    </div>

    <div class="field">
        <label for="firstname">First name</label>
        <input type="text" name="firstname" value="<?php echo escape(Input::get('firstname')); ?>" id="firstname">
    </div>

    <div class="field">
        <label for="lastname">Lastname</label>
        <input type="text" name="lastname" value="<?php echo escape(Input::get('lastname')); ?>" id="lastname">
    </div>

    <div class="field">
        <label for="email">E-mail</label>
        <input type="text" name="email" value="<?php echo escape(Input::get('email')); ?>" id="email">
    </div>

    <div class="field">
        <label for="password">Password</label>
        <input type="password" name="password" id="password">
    </div>

    <div class="field">
        <label for="password_again">Enter your password again</label>
        <input type="password" name="password_again" id="password_again">
    </div>

    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">

    <input type="submit" value="Register">

</form>

都是一页。

感谢您的帮助和耐心。

【问题讨论】:

    标签: php forms submit


    【解决方案1】:

    你为什么不把它分成2个文件?

    • 1 个文件格式 file_form.php
    <form action="file_process.php" method="post">
        <div class="field">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')); ?>" autocomplete="off">
        </div>
    
        <div class="field">
            <label for="firstname">First name</label>
            <input type="text" name="firstname" value="<?php echo escape(Input::get('firstname')); ?>" id="firstname">
        </div>
    
        <div class="field">
            <label for="lastname">Lastname</label>
            <input type="text" name="lastname" value="<?php echo escape(Input::get('lastname')); ?>" id="lastname">
        </div>
    
        <div class="field">
            <label for="email">E-mail</label>
            <input type="text" name="email" value="<?php echo escape(Input::get('email')); ?>" id="email">
        </div>
    
        <div class="field">
            <label for="password">Password</label>
            <input type="password" name="password" id="password">
        </div>
    
        <div class="field">
            <label for="password_again">Enter your password again</label>
            <input type="password" name="password_again" id="password_again">
        </div>
        <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
        <input type="submit" value="Register">
    </form>
    
    • 1个文件进程file_process.php
    <?php
    
        if(Input::exists()) {
            if(Token::check(Input::get('token'))) {
    
                $validate = new Validate();
                $validation = $validate -> check($_POST, array(
                    'username' => array(            // Check name of input
                    'required' => true,         // Add conditions to the inputs
                    'min' => 2,
                    'max' => 20,
                    'unique' => 'users'
                ),
                'firstname' => array(                // Check name of input
                    'required' => false,         // Add conditions to the inputs
                    'min' => 2,
                    'max' => 50,
                    'text' => true
                ),
                'lastname' => array(        // Check name of input
                    'required' => false,        // Add conditions to the inputs
                    'min' => 2,
                    'max' => 50,
                    'text' => true
                ),
                'email' => array(       // Check name of input
                    'required' => true,     // Add conditions to the inputs
                    'email' => true
                ),
                'password' => array(            // Check name of input
                    'required' => true,         // Add conditions to the inputs
                    'min' => 6,
                    'max' => 12,
                    'password_NUM' => true,
                    'password_CAP' => true,
                    'password_LOW' => true,
                    'password_S-CHAR' => true
                ),
                'password_again' => array(      // Check name of input
                    'required' => true,         // Add conditions to the inputs
                    'matches' => 'password'
                )
    
                // If there are more inputs in the form extend to needs
    
                ));
    
                if($validation -> passed()) {
    
                    $user = new User();
    
                    $salt = Hash::salt(32);
    
                    try {
                        $user -> create(array(
                            'username' => Input::get('username'),
                            'firstname' => Input::get('firstname'),
                            'lastname' => Input::get('lastname'),
                            'email' => Input::get('email'),
                            'password' => Hash::make(Input::get('password'), $salt),
                            'salt' => $salt,
                            'joined' => date('Y-m-d H:i:s'),
                            'group' => 1
                        ));
    
                        Session::flash('home', 'You have been registered and can now log in!');
                        Redirect::to('index.php');
    
                        // Add email of confirmation to complete the app
    
                    } catch(Exception $e) {
                        die($e -> getMessage());
                    }
                } else {
                    foreach($validation -> errors() as $error) {
                        echo $error, '<br/>';
                    }
                }
            }
        }
    
    ?>
    
    

    【讨论】:

    • 您好 FakeZero,我尝试了您的建议,但实际上一直在发生相同的情况,因为我将表单引用到另一个文件中,所以我得到一个空白页作为重定向。感谢您的建议,但是,要么它对我不起作用,要么我做错了什么! (很可能是最后一个)再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2017-10-16
    • 2013-02-22
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多