简体   繁体   English

模态表单在提交时显示错误并阻止其关闭

[英]Modal form show error when submitting and prevent it from closing

This is my html code for my modal form. 这是我的模式形式的html代码。

<form  action="" class="cd-form" method="POST" id="signinform" onsubmit="return: false;">
    <p class="fieldset" id="warningPanel">
        <?php ShowAlertWarning(); ?>
    </p> 
    <p class="fieldset">
        <label class="image-replace cd-username" for="signin-username">Username</label>
        <input class="full-width has-padding has-border" id="signin-username" name="login_username" type="text" placeholder="Username" required>
        <span class="cd-error-message">Error message here!</span>
    </p>
    <p class="fieldset">
        <label class="image-replace cd-password" for="signin-password">Password</label>
        <input class="full-width has-padding has-border" id="signin-password" name= "login_password" type="password"  placeholder="Password" required>
        <span class="cd-error-message">Error message here!</span>
    </p>
    <div id="remember">
        <p class="fieldset">
            <input type="checkbox" id="remember-me" >
            <label for="remember-me">Remember me</label>
        </p>
    </div>
    <input class="full-width" type="submit" name="login" value="Login" id="signin_button">
</form>

And this is also my php code. 这也是我的php代码。

if(isset($_POST['login'])){ 
    $username = stripslashes($_POST['login_username']);
    $password = stripslashes($_POST['login_password']);

    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);


    $sql = "SELECT user.userID, user.password, profile.firstname FROM user, profile  WHERE username = '$username' AND user.userID = profile.userID";
    $result = $con->query($sql);

    if($result->num_rows > 0){

        while($row = $result->fetch_assoc()){

            $chk_password = $row['password'];
            $chk_firstname = $row['firstname'];
            $userID = $row['userID'];
            $_SESSION['userID'] = $userID;
            if(password_verify($password, $chk_password)){
                $_SESSION['username_logged_in'] = true;

                if(empty($chk_firstname))
                    header('Location: profile.php');
                else
                    header('Location: ' . $_SERVER['HTTP_REFERER']);
            }
        }
    }
    else{
          $showWarning = true;
          session_destroy();
        }
}

function ShowAlertWarning(){

        global $showWarning;
        $newShowWarning = $showWarning;

        if($newShowWarning){
            echo '<div class="alert alert-dismissible alert-danger form-feedback">';
            echo '<button type="button" class="close" data-dismiss="alert">x</button>';
            echo '<strong>Oh snap!</strong> Wrong username or password, please try again.';
            echo '</div>';          
        }
}

Is there anyway I can show the error inside my modal form without closing it? 无论如何,我可以在不关闭模态表单的情况下显示错误吗? I have this also ShowAlertWarning() function that it will display in my modal form without refreshing my page. 我还有这个ShowAlertWarning()函数,它将以我的模式形式显示而不刷新我的页面。

take type="submit" off of the submit button. 从提交按钮中取消type="submit" That is whats closing the form. 那就是关闭表格的原因。 Then instead add to the input onclick="yourCustomFunctionHere()" and write a function with JS that will check for fields to not be empty and only submit it if they are all full. 然后改为在输入中添加onclick="yourCustomFunctionHere()"并使用JS编写一个函数,该函数将检查字段是否为空,并且仅在字段已满时才提交。

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

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