简体   繁体   English

禁用表单提交 JQuery 上的按钮后,使用按钮名称将表单提交给 PHP

[英]Submit form to PHP with button name after disabling the button on form submit JQuery

I have a form which uses php for form processing.我有一个使用 php 进行表单处理的表单。 I don't the user to submit the form twice.我不让用户提交两次表单。 I've tried to use jquery to disable the form on submit.我尝试使用 jquery 在提交时禁用表单。 but the problem is it disables the button but don't submit the form to php.但问题是它禁用了按钮但不提交表单到 php。

I want to detect the form submission with the submit button name.我想用提交按钮名称检测表单提交。

$('form').submit(function(){
    $(this).find('button[type=submit]').prop('disabled', true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="signup.php" method="post" class="form-horizontal m-t-30" novalidate>

    <div class="form-group">
        <div class="col-12">
            <label for="username">Username</label>
            <input type="text" name="username" placeholder="Choose a Username" maxlength="15" id="username" class="form-control" required>
            <div class="form-errors <?php echo isset($errors['username']) ? 'filled' : ''; ?>"><?php echo $errors['username'] ?? ''; ?></div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-12">
            <label for="email">Email</label>
            <input type="email" name="email" placeholder="Enter your Email address" id="email" class="form-control" required>
            <div class="form-errors <?php echo isset($errors['email']) ? 'filled' : ''; ?>"><?php echo $errors['email'] ?? ''; ?></div>
        </div>
    </div>                            

    <div class="form-group">
        <div class="col-12">
            <label for="password">Password</label>
            <input type="password" name="password" placeholder="Enter password" id="password" class="form-control" required>
            <div class="form-errors <?php echo isset($errors['password']) ? 'filled' : ''; ?>"><?php echo $errors['password'] ?? ''; ?></div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-12">
            <p class="font-12">By registering you agree to the Pawnhost Terms of Use</p>
        </div>
    </div>

    <div class="form-group text-center m-t-20">
        <div class="col-12">
            <button type="submit" name="signup-submit" id="btnSubmit" class="btn btn-block btn-primary w-md waves-effect waves-light">
                <span id="btnLoading" class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="display: none;"></span>
                <span id="btnText">Create My Account</span>
            </button>
        </div>
    </div>

    <div class="form-group row m-b-0 mt-4">
        <div class="col-12 text-center">
            <a href="login.php" class="text-muted">Already have an account? Login</a>
        </div>                              
    </div>
</form>

You should set the attribute, not the property.您应该设置属性,而不是属性。

$('form').on('submit', function(event){ $(this).find('button[type=submit]').attr('disabled', 'disabled'); });

OP also wanted to include the button in the form submission. OP 还希望在表单提交中包含该按钮。 This can be achieve by instead of using a button, using an input tag.这可以通过使用输入标签而不是使用按钮来实现。

<input type="submit" name="mybtn" value="myval" />

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

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