此表单正在提交但不应提交

时间:2015-01-21 21:04:52

标签: javascript php html forms post

我正在网站上工作,我有这个change_password.php脚本,这是一个3个字段,用户第二次输入他的上一个密码,新密码和新密码。如果第二次和第三次匹配,我已尝试进行验证。如果他们没有向用户显示提醒。

问题是它确实在密码不同时显示警告,但无论如何它都会重定向到下一页do_change_password.php。我该怎么做才能解决它?

<html>
<?php
session_start(); // começa a session
require "config.php";

if(Check_Login_Status())
{
    Update_Login_Status();
}
else
{
    session_unset();
    phpAlert_Redirect("ACESSO NEGADO", "index.php");
    exit();
}
?>
<head>

    <meta charset = 'UTF-8'>
    <link rel="shortcut icon" href="images/favicon.ico"/>
    <title>Sistema de Estágios - UFMS - Login</title>
    <link href = "css/bootstrap.css" rel = "stylesheet" >
    <link href = "css/index.css" rel = "stylesheet" >
    <script src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $("input").blur(function()
            {
                if($(this).val() == "")
                {
                    $(this).css({"border" : "1px solid #F00"});
                }
                else
                    $(this).css({"border" : "1px solid #FFF"});
            });

            $("#botao").click(function()
            {
                var cont = 0;
                $("#form input").each(function()
                {
                    if($(this).val() == "")
                        cont++;
                });

                if(cont == 0)
                {
                    var x = document.forms["Form"]["new_pass"].value;
                    var y = document.forms["Form"]["conf_pass"].value;
                    if(x == y)
                        $("#form").submit();
                    else
                        alert("Senhas não conferem");
                }
            });
        });
    </script>
</head>

<body>
    <?php
    Set_Barra_Superior($_SESSION["auto"]);
    ?>
    <center>
        <div class = "container">   
            <div class = "principal"> 
                <form id="form" name="Form" method="post" action="do_change_password.php">
                    <p>
                        <label for="a">Senha antiga:</label>
                        <input id="a" name="old_pass" type="password" class="form-control"/><br/>
                    </p>
                    <p>
                        <label for="b">Nova Senha:</label>
                        <input id="b" name="new_pass" type="password" class="form-control"/><br/>
                    </p>
                    <p>
                        <label for="c">Confere nova senha:</label>
                        <input id="c" name="conf_pass" type="password" class="form-control"/><br/>
                    </p>
                    <button id="botao" name="botao" value="login" class="btn btn-primary" style="width: 100%;">Login</button>
                </form>
            </div>
        </div>
    </center>
</body>

2 个答案:

答案 0 :(得分:1)

将事件添加到点击功能中,在事件上调用preventDefault,然后return false

$("#botao").click(function(e)
{
     e.preventDefault();    
     var cont = 0;
     $("#form input").each(function()
     {
         if($(this).val() == "")
             cont++;
         });

    if(cont == 0)
    {
        var x = document.forms["Form"]["new_pass"].value;
        var y = document.forms["Form"]["conf_pass"].value;
        if(x == y)
            $("#form").submit();
        else
            alert("Senhas não conferem");
    }
    return false;
});

答案 1 :(得分:1)

尝试返回false,如:

if(cont == 0)
{
    var x = document.forms["Form"]["new_pass"].value;
    var y = document.forms["Form"]["conf_pass"].value;
    if(x != y)
    {
        alert("Senhas não conferem");
        return false;
    }
}