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

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 = '<p>' + message + '</p><p>';</p>

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

Categories: Javascript