A lightweight handler for submitting HTML forms via AJAX
npm install @dpsys/ajax-form


A lightweight handler for submitting HTML forms via AJAX.
Supports CommonJS (CJS) and ES Modules (ESM).
``bash`
npm i @dpsys/ajax-form
###
###
html
`
###
###
$3
This example uses Axios. Feel free to use any other AJAX implementation.
` javascript
import ajaxForm from '@dpsys/ajax-form';
import axios from 'axios';ajaxForm('my_form').setSubmitCallback( (axForm, formData) =>
{
axios.post('/some-route', formData)
.then( (resp) =>
{
if (resp.data.formErrors)
{
axForm.showErrors(resp.data.formErrors);
}
else if (resp.data.success)
{
// Do something...
axForm.reset();
}
});
});
`
###
###
Reference
$3
Creates and returns an AjaxForm instance for handling the specified form.
##### Parameters:
-
form: A form element (HTMLElement), CSS selector (string), or form name (string).##### Returns: An AjaxForm instance.
##### Throws: Error if the form is not found or invalid.
___
$3
___
#### setSubmitCallback(callback)Sets a callback function to handle form submission.
##### Parameters:
-
callback(axForm, formData): Function called on submit. axForm is the instance, formData is a FormData object.
___
#### showErrors(errors)Displays error messages by inserting
elements after input fields.##### Parameters:
-
errors: Array of objects with {field_id: string, message: string}`.Returns the form element.
___
#### reset()
Resets the form to its default values.
___