简体   繁体   English

通过Ajax以引导方式提交更改密码表单

[英]Submit change password form in bootstrap modal through ajax

I Have a change password form which I have tried to code so that it gets submitted through ajax. 我有一个尝试更改代码的更改密码表单,以便它可以通过ajax提交。 I needed to do validation too. 我也需要进行验证。 Below is the code that I've written. 以下是我编写的代码。 Is there anyway so that we can use this js ajax function for multiple modal forms? 无论如何,我们可以将此js ajax函数用于多种模式形式吗? Or will we need to create a seperate function for submitting each modal form? 还是我们需要创建一个单独的函数来提交每种模式形式?

Also I wanted to make the parent page reload after user closes the modal so I have added this code: 我也想在用户关闭模式后重新加载父页面,所以我添加了以下代码:

$('#edit').on('hidden.bs.modal', function() {
    location.reload();
});

but it reloads the page when someone clicks cancel button too. 但是当有人也单击“取消”按钮时,它会重新加载页面。 Is there any way to prevent reloading when clicking cancel button and only do reloading only by clicking "x". 单击取消按钮时,是否有任何方法可以防止重新加载,并且只能通过单击“ x”来进行重新加载。

Here is the code 这是代码

index.php file where the modal is 模态所在的index.php文件

<p data-placement="top" data-toggle="tooltip" title="Edit" data-original-title="Edit">
    <button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" data-backdrop="static" data-keyboard="false">
        <span class="glyphicon glyphicon-pencil"> Edit</span>
    </button>
</p>

<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Edit Your Detail</h4>
            </div>
            <!--/.modal-header-->

            <div class="modal-body">
                <form method="post" id="updateForm" action="update-info.php">
                    <input type="hidden" name="userID" value="<?php echo $_SESSION['user']; ?>" />
                    <div class="form-group">
                        <label for="customer_name">Customer Name :</label>
                        <input class="form-control" type="text" name="customer_name" id="customer_name" value="<?php echo $userRow['fullName']; ?>" />
                    </div>

                    <h4><u><strong>Change Password</strong></u></h4>
                    <div class="form-group" id="currentPass-group">
                        <label for="current_pass">Current Password :</label>
                        <input class="form-control" type="password" name="current_pass" id="current_pass">
                    </div>

                    <div class="form-group">
                        <label for="new_pass">New Password :</label>
                        <input class="form-control" type="password" name="new_pass" id="new_pass">
                    </div>

                    <div class="form-group">
                        <label for="confirm_pass">Confirm Password :</label>
                        <input class="form-control" type="password" name="confirm_pass" id="confirm_pass">
                    </div>

                    <div class="modal-footer">
                        <!-- <input type="submit" name="submit" class="btn btn-block btn-warning" value="Save changes" /> -->
                        <button type="submit" name="submit" class="btn btn-success" id="submitForm" value="Save changes">Save Changes</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    </div>
                </form>
            </div>

        </div>
    </div>
</div>
<!--/.modal -->

custom.js file: custom.js文件:

$('#edit').on('hidden.bs.modal', function() {
    location.reload();
});

/* must apply only after HTML has loaded */
$(document).ready(function() {
    $("#updateForm").on("submit", function(e) {

        $(".error").hide();
        var hasError = false;
        var currentpass = $("#current_pass").val();
        var newpass = $("#new_pass").val();
        var cnfpass = $("#confirm_pass").val();

        if (currentpass == '') {
            $("#current_pass").after('<span class="error text-danger"><em>Please  enter your current password.</em></span>');
            //$('#currentPass-group').addClass('has-error'); // add the error class to show red input
            //$('#current_pass').append('<div class="help-block">Please enter your current password.</div>'); // add the actual error message under our input
            hasError = true;
        } else if (newpass == '') {
            $("#new_pass").after('<span class="error text-danger"><em>Please enter a password.</em></span>');
            hasError = true;
        } else if (cnfpass == '') {
            $("#confirm_pass").after('<span class="error text-danger"><em>Please re-enter your password.</em></span>');
            hasError = true;
        } else if (newpass != cnfpass) {
            $("#confirm_pass").after('<span class="error text-danger"><em>Passwords do not match.</em></span>');
            hasError = true;
        }

        if (hasError == true) {
            return false;
        }
        if (hasError == false) {

            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");
            $.ajax({
                url: formURL,
                type: "POST",
                data: postData,
                success: function(data, textStatus, jqXHR) {
                    $('#edit .modal-header .modal-title').html("Result");
                    $('#edit .modal-body').html(data);
                    $("#submitForm").remove();
                    //document.location.reload();
                },
                error: function(jqXHR, status, error) {
                    console.log(status + ": " + error);
                }
            });
            e.preventDefault();
        }
    });

    $("#submitForm").on('click', function() {
        $("#updateForm").submit();
    });
});

update-info.php update-info.php

To use this code for multiple form add ajax code in one function and call that function whenever you want to. 要将此代码用于多种形式,请在一个函数中添加ajax代码,并在需要时调用该函数。

To prevent page from reloading when someone click on cancel Instead of using 为了防止有人单击“取消”时重新加载页面,而不是使用

 $('#edit').on('hidden.bs.modal', function () {
    location.reload();
    }); 

Add one click event on cross and then reload page by location.reload(); 在十字架上添加一键事件,然后按location.reload();重新加载页面。

You can use e.preventDefault(); 您可以使用e.preventDefault(); and instead of submit use click event 而不是提交使用点击事件

$("#submitForm").on("click", function(e) {   
         e.preventDefault();

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

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