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

时间:2015-12-17 09:12:35

标签: javascript php jquery html twitter-bootstrap

这是我的模态表单的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>

这也是我的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>';          
        }
}

无论如何,我可以在我的模态形式中显示错误 关闭它?我还有ShowAlertWarning()这个功能 它将以我的模态形式显示而不刷新我的页面。

1 个答案:

答案 0 :(得分:0)

从提交按钮中删除type="submit"。那就是关闭表格的原因。然后改为添加到输入onclick="yourCustomFunctionHere()"中,并使用JS编写一个函数,该函数将检查字段是否为空,并仅在字段已满时才提交。