不能用ajax提交我的表单两次

Can't submit my form twice with ajax

本文关键字:两次 表单 ajax 提交 我的 不能      更新时间:2023-09-26

大家好,我有一些问题(我认为简单)表单提交与ajax。

用户第一次提交表单时,一切正常:div的内容改变,php被处理。但是如果在我的数据库中没有匹配,它将返回"f", javascript将写回一个无法使用的表单,不能用ajax重新提交。

HTML:

<div class="maincontainer" id="login">
  <form id="loginform" onsubmit="void login();return false;">
    <input name="username" type="text" class="textboxinput"  id="username"  autocomplete="off" />
    <input name="password" type="password" class="textboxinput" id="password"  autocomplete="off" />
    <input type="submit" name="button" class="button" id="button" value="Login" /> 
  </form>
</div>
Javascript:

function login(){
    //Change the box content to "Logging in"
    $("#login").html("<p style='line-height:170px;text-align:center;width:100%;margin:0px;padding:0px;'>Logging in</p>");
    //Get the values of the username and password field
var username = $('#username').val();
var password = $('#password').val();
    //Make the ajax request
$.ajax({        
        datatype: "text",
        type: "POST",
        url: "process.php",
        data: "username=" + username + "&password=" + password,
        success: function (m) {
        //If there's no match, rewrite the form in the div  
            if ( m == "f"){
                content= "  <form id='loginform' onsubmit='void login();return false;'><input name='username' type='text' class='textboxinput' id='username' autocomplete='off' /><input name='password' type='password' class='textboxinput' id='password' autocomplete='off' /><input type='submit' name='button' class='button' id='button' value='Login' /> <p style='font-size:small;'>Login failed, please try again</p></form>";         
                $("#login").html(content);
            }
        }
    });

};

编辑

我没有找到问题,但我找到了一个解决方案,那就是不擦除我的表单,只是隐藏它

HTML:

<div class="maincontainer" id="login">
    <div id="errorbox"></div>
     <form id="loginform" onsubmit="return login();">
    <input name="username" type="text" class="textboxinput username"  id="username" value="username"  onfocus="if(this.value==this.defaultValue) this.value='';"   onblur="if(this.value=='') this.value='username'" autocomplete="off" />
    <input name="password" type="password" class="textboxinput password" id="password" value="password"  onfocus="if(this.value==this.defaultValue) this.value='';"  onblur="if(this.value=='') this.value='password'" autocomplete="off" />
    <input type="submit" name="button" class="button" id="button" value="Login" /> 
    <div id="errorshow"><a href="register.php">Register</a><a href="credsretrieve.php">Forgot your login infos?</a></div>
  </form>
</div>
Javascript:

function login(){
    var username = $('#username').val();
    var password = $('#password').val();
//Do security checks here

    $("#loginform").hide();
    $("#errorbox").html("<p class='logloading'>Logging in</p>");
    $.ajax({        
        datatype: "text",
        type: "POST",
        url: "login.php",
        data: "username=" + username + "&password=" + password + "&method=js",
        success: function (m) {
            if ( m == "f"){ 
                $("#errorbox").html("");
                $('#username').val("");
                $('#password').val("");
                $("#loginform").show();
                $("#errorshow").html("<p class='errormsg'>Wrong username/password combination</p>");
                $("#username").focus();
            }
            else{
                window.location = m;                
            }
        }
    });
    return false;
};

哦,我应该感谢每个在这里评论(和发帖)的人,每一个信息都对我有一点帮助。

只是一个修正,不是对你最初问题的明确回答。

我使用

data: $('#form_id').serialize()

而不是手动完成所有这些:

data: "username=" + username + "&password=" + password,

当用户提交一个空格时会发生什么?您的查询中断。

只需确保用户名框的名称为username,密码框也遵循相同的模式。


编辑

这段代码有点奇怪:

onsubmit='void login();return false;'

不要手动输入这个,试试下面的代码(只是删除那部分并插入这个):

$('#loginform').live('submit', function(e) {
  login();
  e.preventDefault()
});

我想我之前也遇到过这个问题。

一个可行的解决方法是将登录函数放在提交按钮的onclick上(并且也返回false)