All Projects → claviska → jquery-ajaxSubmit

claviska / jquery-ajaxSubmit

Licence: MIT License
Effortlessly submit forms using AJAX and JSON.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to jquery-ajaxSubmit

yii2-forms
Forms CRUD - formbuilder, generator code
Stars: ✭ 32 (-17.95%)
Mutual labels:  forms, ajax
Uploader
A lightweight and very configurable jQuery plugin for file uploading using ajax(a sync); includes support for queues, progress tracking and drag and drop.
Stars: ✭ 1,042 (+2571.79%)
Mutual labels:  forms, ajax
form.js
🖍️ Automagic forms.
Stars: ✭ 16 (-58.97%)
Mutual labels:  forms, ajax
yii2-ajaxform
submit ActiveForm via ajax
Stars: ✭ 20 (-48.72%)
Mutual labels:  ajax
php-ajax
A simple Ajax App with PHP backend
Stars: ✭ 53 (+35.9%)
Mutual labels:  ajax
CustomFormViews
A clean collection of views used for forms.
Stars: ✭ 12 (-69.23%)
Mutual labels:  forms
desafiosInternos
Resultado da peregrinação com resolução de dúvidas interessantes
Stars: ✭ 15 (-61.54%)
Mutual labels:  ajax
yii-fullajax
fullajax Yii playground
Stars: ✭ 21 (-46.15%)
Mutual labels:  ajax
Exopite-Multifilter-Multi-Sorter-WordPress-Plugin
Display and/or sort/filter any page or post types by multiple taxonomies or terms (like post by categories and/or tags) with AJAX. Exopite multifilter, multi-sortable, multi selectable, multi filterable sortable Wordpress Plugin.
Stars: ✭ 18 (-53.85%)
Mutual labels:  ajax
Contact-Form-PHP
Simple and secure contact form using Ajax, validations inputs, SMTP protocol and Google reCAPTCHA v3 in PHP.
Stars: ✭ 28 (-28.21%)
Mutual labels:  ajax
form-js
View and visually edit JSON-based forms.
Stars: ✭ 125 (+220.51%)
Mutual labels:  forms
axios-endpoints
Axios endpoints helps you to create a more concise endpoint mapping with axios.
Stars: ✭ 41 (+5.13%)
Mutual labels:  ajax
bootstrap-add-clear
bootstrap plugin to add a (x) clear button to your input fields
Stars: ✭ 36 (-7.69%)
Mutual labels:  forms
Toast
To use it in PCL or .NetStandard projects write this line of code : CrossToastPopUp.Current.ShowToastMessage("Message");
Stars: ✭ 51 (+30.77%)
Mutual labels:  forms
valid8
Valid8 - Super Simple Bootstrap Form Valiation
Stars: ✭ 17 (-56.41%)
Mutual labels:  forms
django-semanticui-forms
Effortlessly style all of your Django forms and form fields with Semantic UI wrappers.
Stars: ✭ 59 (+51.28%)
Mutual labels:  forms
django-formidable
On the way to glory! again!
Stars: ✭ 19 (-51.28%)
Mutual labels:  forms
formio
Formio, form definition and binding library for Java platform
Stars: ✭ 24 (-38.46%)
Mutual labels:  forms
micell
A collection of functions for front-end development
Stars: ✭ 16 (-58.97%)
Mutual labels:  ajax
TableBundle
Symfony Bundle for easy pagination and filtering
Stars: ✭ 24 (-38.46%)
Mutual labels:  ajax

jquery.ajaxSubmit.js - Effortlessly submit forms using AJAX and JSON

Developed by Cory LaViska for A Beautiful Site, LLC

Licensed under the MIT license: http://opensource.org/licenses/MIT

Overview:

This plugin provides a minimal, lightweight solution to submit forms using AJAX and JSON. There is no client side validation included, as that is outside the scope of this plugin. All validation is expected to happen on the server.

Features:

  • Makes regular HTML forms AJAX-capable.
  • Minimal markup.
  • Handles form serialization so you don't have to.
  • Shows/hides a loader and message if you provide them.
  • Highlights invalid fields.
  • Callbacks for success, error, before, and after.
  • API to disable/enable form inputs.
  • API to reset the form (including loader, message, and invalid fields)
  • Compact! (about 240 lines)

Installing

Include the minified version of this plugin in your project or install via NPM:

npm install --save @claviska/jquery-ajaxSubmit

Form syntax

Create a form as you normally would in HTML. By default, the action and method attributes will be used as the target URL and method for the AJAX request. Alternatively, you can specify them as options (see below for details).

You can optionally add a loader container anywhere inside the form that will be shown/hidden automatically when the form is submitted:

<div class="form-loader">
  <img src="loader.gif">
</div>

You can optionally add a message container anywhere inside the form that will be shown/hidden and populated automatically when the form receives a message from the server:

<div class="form-message"></div>

Attaching the plugin

Minimal example that logs the server's response if successful:

Minimal example with success callback:

$('form').ajaxSubmit({
  success: function(res) {
    console.log(res);
  }
});

Example with all possible options and callbacks:

$('form').ajaxSubmit({
    // Options (default values shown)
    data: function() {
      return $(this).serialize();
    },
    headers: {
      'My-Custom-Header': 'Value'
    },
    hideInvalid: function(input) {
      $(input).closest('.form-group').removeClass('has-warning');
    },
    loader: '.form-loader',
    message: '.form-message',
    messageErrorClasses: 'message-error',
    messageSuccessClasses: 'message-success',
    method: function() {
      return $(this).attr('method');
    },
    showInvalid: function(input) {
      $(input).closest('.form-group').addClass('has-warning');
    },
    url: function() {
      return $(this).attr('action');
    },

    // Callbacks
    after: function(res) { ... },
    before: function() { ... },
    error: function(res) { ... },
    success: function(res) { ... }
});

Options

  • data: The data to send to the server. This option can also be a function that returns data. By default, it uses the form's serialized data.
  • headers: Custom headers to send along with the request.
  • hideInvalid: A function to be called on all invalid inputs to effectively undo the changes made by the showInvalid function.
  • loader: A selector that points to the form's loader. This will be shown/hidden automatically using the HTML hidden property. Defaults to form-loader.
  • message: A selector that points to the form's message container. This will be shown/hidden automatically using the HTML hidden property. Defaults to form-message.
  • messageErrorClasses: One or more space-separated classes to attach to the message container when the response is erroneous. Defaults to message-error.
  • messageSuccessClasses: One or more space-separated classes to attach to the message container when the response is successful. Defaults to message-success.
  • method: The method to use (i.e. GET or POST). This option can also be a function that returns the method. By default, it uses the form's method attribute.
  • showInvalid: A function to be called on all invalid inputs as returned by res.invalid. Use it to apply error styles, etc. The default behavior is compatible with Bootstrap 4 beta and will add the is-invalid to the input.
  • url: The URL to send the request to. This option can also be a function that returns the URL. By default, it uses the form's action attribute.

You may also update the default options before instantiation:

$.ajaxSubmit.defaults.optionName = yourValue;

Callbacks

All callbacks are called in the context of the respective form (i.e. refer to the form using this inside your callbacks).

The success and after callbacks return the server's JSON response as their first argument.

  • after(res): runs after the request has completed and your server returns a response.
  • before(): runs before the request is sent. Returning false will cancel submission.
  • error(res, err): runs when your server returns an unsuccessful response (e.g. 400 BAD REQUEST).
  • success(res): runs when your server returns a successful response (e.g. 200 OK).

Methods

Methods are called using this syntax:

$('form').ajaxSubmit('method', arg);

The following API methods are supported:

  • busy: sets the form's busy state. Pass in true or false.
  • create (default): initializes the plugin.
  • destroy: returns the form to its pre-initialized state.
  • disable: disables/enables all inputs. Pass in true or false.
  • reset: resets the form to its original state, including input values, loaders, and messages.

Responding from the server

Your server should return a well-formed JSON response with an appropriate HTTP status code. The response can contain any data you want, but there are a couple reserved properties:

{
  "invalid": ["username", "password"],
  "message": "Invalid username or password"
}
  • invalid: Optional. An array of field names to be marked erroneous by the plugin.

  • message: Optional. A string of plain text that will be injected into the message container.

Node.js + Express

res.status(400).json({
  invalid: ['username', 'password'],
  message: 'Invalid username or password'
});

PHP

In PHP, you can return a JSON response like this:

<?php
http_response_code(400); // Bad Request
header('Content-Type: application/json'); // Send as JSON
exit(json_encode([
  'invalid': ['username', 'password'],
  'message': 'Invalid username or password'
]));
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].