繁体   English   中英

使用 Bootstrap Modal 进行模态验证 - PHP

[英]Modal validation using Bootstrap Modal - PHP

PHP 脚本和验证:

    function SampleInput($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    
    if (isset($_POST['RegisterClient'])) {

        $file = $_FILES['avatar']['name'];
        $file_location = $_FILES['avatar']['tmp_name'];
        $folder = "../Public/img/uploads/profile-picture/";
        $new_file_name = strtolower($file);
        $final_file = str_replace(' ', '-', $new_file_name);

        $name       = htmlspecialchars(trim($_POST['name'], ENT_QUOTES));
        $username   = htmlspecialchars(trim($_POST['username'], ENT_QUOTES));
        $email      = htmlspecialchars(trim($_POST['email'], ENT_QUOTES));
        $password   = htmlspecialchars(trim($_POST['password'], ENT_QUOTES));
        $cpassword  = htmlspecialchars(trim($_POST['cpassword'], ENT_QUOTES));
        $role       = htmlspecialchars(trim($_POST['role'], ENT_QUOTES));

        if (move_uploaded_file($file_location, $folder.$final_file)) {
            $image = $final_file;
        }

        if(empty($_POST['name']) || empty($_POST['username']) || empty($_POST['password']) || empty($_POST['cpassword']) || empty($_POST['role'])) {
            $error = "All fields are required!"; 
        } else {
            $name = SampleInput($_POST['name']);
            $username = SampleInput($_POST['username']);
            $password = SampleInput($_POST['password']);
            $cpassword =SampleInput($_POST['cpassword']);
            $role = SampleInput($_POST['role']);
            if (!preg_match('/^[a-zA-Z0-9\s]+$/',$name && $username && $password && $cpassword && $role)) {
                $error = "Only letters and white space allowed";
            }
        }

        $stmt = $db->prepare("INSERT INTO clients(name, username, email, password, avatar, role, registration_date) VALUES (:name, :username, :email, :password, :avatar, :role, NOW())");
        $stmt->execute(array(
            'name' => $name,
            'username' => $username,
            'email' => $email,
            'password' => $password,
            'avatar' => $image,
            'role' => $role
        ));

        $success = 'Registered!';
    }

模态:

<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <form action="" method="POST" enctype="multipart/form-data">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Create user</h5>
                        <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
                        </button>
                    </div>
                    <div class="modal-body">
                            <?php
                                if ($error) {
                                    echo '<div class="mb-3">' . $error . '</div>';
                                } else if ($success) {
                                    echo '<div class="mb-3">' . $success . '</div>';
                                }
                            ?>
                            <div class="row gx-3 mb-3">
                                <div class="col-md-12">
                                    <label class="small mb-1" for="login-fullname">Full name*</label>
                                    <input class="form-control" name="name" type="text" placeholder="Valid full name" />
                                </div>
                            </div>
                            <div class="mb-3">
                                <label class="small mb-1" for="login-username">Username (how your name will appear to other users on the site)</label>
                                <input class="form-control" name="username" type="text" placeholder="Valid username" />
                            </div>
                            <div class="mb-3">
                                <label class="small mb-1" for="login-email">Email address</label>
                                <input class="form-control" name="email" type="text" placeholder="Valid email" />
                            </div>
                            <div class="row gx-3 mb-3">
                                <div class="col-md-6">
                                    <label class="small mb-1" for="login-password">Password</label>
                                    <input class="form-control" name="password" type="password" placeholder="Strong password" />
                                </div>
                                <div class="col-md-6">
                                    <label class="small mb-1" for="login-cpassword">Confirm Password</label>
                                    <input class="form-control" name="cpassword" type="password" placeholder="Strong password" />
                                </div>
                            </div>
                            <div class="row gx-3 mb-3">
                                <div class="card-body text-center">
                                    <input type="file" required class="btn btn-primary" name="avatar">
                                    <div class="small font-italic text-muted mb-6">JPG or PNG no larger than 5 MB</div>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label class="small mb-1" for="selectRole">Select user role</label>
                                <select class="form-control" name="role">
                                    <option value="">Select user role</option>
                                    <option value="admin">Admin</option>
                                    <option value="user">User</option>
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
                            <button type="submit" name="RegisterClient" class="btn btn-primary">Create</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

我不知道问题出在哪里,即使问题或表单已成功提交,我也希望模态保持打开状态。 现在,使用这段代码,如果提交表单有问题,模式会关闭,如果我们再次打开它,用户可以看到错误消息。 我哪里错了?

一整天后,我解决了这个问题。

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
})

使用上面编写的这段代码,只有当用户单击关闭按钮时,模态框才会关闭。 使用此代码,您可以通过按键盘上的 ESC 按钮禁用关闭模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM