jQuery.ajax - Submit All Inputs In Form Without The Need To Manually Enter Them

Posted 11 months ago

Use serialize


data :   $("form").serialize()

So your Ajax call might look something like this


            let ajax = new XMLHttpRequest();
            let form = $(this);

            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: form.serialize(),
                success: function (data) {
                    const { success, message } = data;

                    let response = this.querySelector('.response');
                    response.innerHTML = '

' + message + '

';

if (success) form.style.display = 'none'
},
error: function (err) {
console.warn('Error')
console.warn(err)
}
});