Search code examples
javascriptjquerytwitter-bootstrapjqbootstrapvalidation

Bootstrap Validator - Send all input field values to remote PHP file


I was wondering if there was a way to send all of my input field values to a remote PHP file using Bootstrap Validator.

To explain, I have two input fields in a log in form. I use Bootstrap Validator's remote validation on both of the input fields. Each validation only sends the requested input field's value and there is no access to the other input field via $_POST in the PHP back end.

I know that I can send data to the PHP back end with the data option, but I am unsure as to how the formatting would go if I were to send the values of different input fields in that section.

Here is a coding example.

$(document).ready(function() {
    $('#login_form').bootstrapValidator({
        fields: {
            username: {
                message: 'Invalid',
                validators: {
                    remote: {
                        message: 'Invalid',
                        url: '/path/to/backend/',
                        data: {
                            password: // What do I put here?
                        }
                    }
                }
            },
            password: {
                message: 'Invalid',
                validators: {
                    remote: {
                        message: 'Invalid',
                        url: '/path/to/backend/',
                        data: {
                            username: // What do I put here?
                        }
                    }
                }
            }
        }
    });
});

That coding example is one way to solve my problem and have both input field values submitted to the back end. If anyone has a solution to my problem with this method, or another way that I can go about giving all input field values to the back end, please let me know.

Thank you in advance for any help that you may give.


Solution

  • For sending any input field in your form to the backend, just call a validator function that recalls the values of the input fields in your form and assign them to a parameter with a descriptive name. This parameter then can be accessed from POST (or from GET i you so configure it) in /path/to/backend.

    You can see you code modified below. I hope it will work for you. It's fully tested code.

    Please send feedback.

    $(document).ready(function() {
    $('#login_form').bootstrapValidator({
        fields: {
            username: {
                message: 'Invalid',
                validators: {
                    remote: {
                        url: '/path/to/backend/',
                        data: function(validator) {
                           return {
                               password: $('[name="passwordNameAttributeInYourForm"]').val(),
                               whatever: $('[name="whateverNameAttributeInYourForm"]').val()
                           };
                        },
                       message: 'Invalid'
                    }
                }
            },
            password: {
                message: 'Invalid',
                validators: {
                    remote: {
                        url: '/path/to/backend/',
                        data: function(validator) {
                           return {
                               username: $('[name="usernameNameAttributeInYourForm"]').val(),
                               whatever: $('[name="whateverNameAttributeInYourForm"]').val()
                           };
                        },
                       message: 'Invalid'
                    }
                }
            }
        }
    });
    

    });