简体   繁体   English

表单未通过 Ajax 提交“提交”输入

[英]Form not submitting “Submit” input via Ajax

Issue问题

I have a form constructed using Bootstrap 3.我有一个使用 Bootstrap 3 构建的表单。

I post the data via Ajax to a PHP page "formhandle.php", which is used to handle all forms on the site.我通过 Ajax 将数据post到 PHP 页面“formhandle.php”,该页面用于处理站点上的所有表单。

However, the submit input information is not being sent.但是,未发送submit输入信息。

HTML Form HTML 表单

<form role="form" id="frmLogin" action="dashboard/inc/formhandle.php">

    <p class="text-danger frmLogin_error" style="display:none;">Invalid username/password combination</p>

    <div class="form-group">
        <label for="frmLogin_username">Username/domain</label>
        <input id="frmLogin_username" name="username" type="text" class="form-control" placeholder="example.com">
    </div>

    <div class="form-group">
        <label for="frmLogin_password">Password</label>
        <input id="frmLogin_password" name="password" type="password" class="form-control" placeholder="Password">
    </div>

    <input type="submit" name="frmLogin_submit" value="Login" class="btn btn-default" />

</form>

jQuery/Ajax Submission jQuery/Ajax 提交

$('form').submit( function(e) {

    var formName = $(this).attr('id');
    $('#'+formName).find('.'+formName+'_error').hide();

    $(this).find('input[type=submit]').prop('disabled',true);

    var postData = $(this).serializeArray();
    var formURL = $(this).attr("action");

    $.ajax({
        url : formURL,
        type: "POST",
        //async: false,
        data : new FormData(this),
        success:function(data, textStatus, jqXHR) {
            console.log(data);
            if(data=='success'){
                console.log('success');
            } else {
                $('#'+formName).find('.'+formName+'_error').show();
            }
            $('#'+formName).find('input[type=submit]').prop('disabled',false);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('Error: An unknown error occurred.');
        },
        cache: false,
        contentType: false,
        processData: false
    });

    e.preventDefault();
});

PHP Code PHP代码

require_once('connect.php');
require_once('functions.php');

exit(print_r($_POST));

if(isset($_POST['frmLogin_submit'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $dbh->prepare('SELECT * FROM `users` 
                            WHERE (`username`=? AND `password`=?)
                            OR (`email`=? AND `password`=?');
    if($stmt->execute(array($_POST['username'],$_POST['password'],$_POST['username'],$_POST['password']))){
        if($stmt->rowCount()>1) exit('error: invalid data');
        $userData = $stmt->fetch(PDO::FETCH_ASSOC);
        validateUser($userData['id']);
        exit('success');
    }
}

Console Output控制台输出

Array
(
    [username] => username_input
    [password] => password_input
)
1

What Console Output I Expect我期望什么控制台输出

Array
(
    [username] => username_input
    [password] => password_input
    [frmLogin_submit] => Login
)
1

Why isn't the frmLogin_submit input value being posted?为什么不发布frmLogin_submit输入值?

Because you have disabled the input[type="submit"]因为您已禁用input[type="submit"]

From your code:从您的代码:

 $(this).find('input[type=submit]').prop('disabled',true);

Disabled inputs,textareas are not submitted with the form.禁用输入,文本区域不与表单一起提交。

Suggestion: Set readonly to the input, if you don't want the user to interact with the button.建议:如果您不希望用户与按钮交互,请将readonly设置为输入。

$(this).find('input[type=submit]').prop('readonly',true);

it is like a billion years from the time this question was share but hope this helps someone.从这个问题被分享的时间算起已经有 10 亿年了,但希望这对某人有所帮助。 i had same problem, and this is the solution i used:我有同样的问题,这是我使用的解决方案:

    $.ajax({
       type: "POST",
       url: url,
       data: form.serialize()+"&submit="+document.activeElement.value,

don't know how risky this might be, but all things being equal, this seems to be a quick fix to the problem.不知道这可能有多大风险,但在所有条件相同的情况下,这似乎是解决问题的快速方法。

Edited: Someone contacted me to complain that this code was not working.编辑:有人联系我抱怨此代码不起作用。

After I reviewed his code, I saw that he had used a different name for the submit input which was not "submit", So I'm here to review my answer to make it more dynamic.在我查看了他的代码后,我看到他对提交输入使用了不同的名称,而不是“提交”,所以我在这里查看我的答案以使其更具动态性。

    $.ajax({
       type: "POST",
       url: url,
       data: form.serialize()+"&"+document.activeElement.name+"="+document.activeElement.value,

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

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