Skip to content

Commit b6cc809

Browse files
committed
Change default action of XHRForm onSuccess to display a message in an alert - add to documentation
1 parent bbfeef8 commit b6cc809

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Many plugins and libraries are too generic, verbose, bulky, lacking, or have a l
77
Much of what is included here is stuff that I've written several times, in several different ways, using jQuery in the past (or wrapping other jQuery plugins).
88
The goal of this library is to allow me (and you, now that it's open source) to integrate slim, mostly dependency-free, components as-needed with more specific use-cases than what is currently offered elsewhere.
99

10-
...where else can you get a component to grab a form from another page and stick it on the current one with XHR and XHR submission in [6 lines of custom JS](#6lineofjs)?
10+
...where else can you get a component to grab a form from another page and stick it on the current one with XHR and XHR submission in [3 lines of custom JS](#3lineofjs)?
1111

1212
<h1 id="whatsincluded">What's Included</h1>
1313

@@ -264,8 +264,11 @@ var remote_form = new XHRForm(document.getElementById('my-form'), {
264264
xhrSubmit: true, //wouldn't make a whole lotta sent to use this if this were false lol, but it's here for extending classes and incase you want to toggle it for whatever reason
265265
submitURL:null, //when null, the form's action will be used (if explicitly defined), otherwise it falls back to the URL the form was retrieved from
266266
submitMethod:null, //when null, the form's method will be used (if explicitly defined), otherwise it falls back to POST
267-
onError: function(error, response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
268-
onSuccess: function(response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
267+
onError: function(error, response, form){ alert(error); }, //although you can add more, you can only pass 1 to start with in the constructor
268+
onSuccess: function(response, form){ //although you can add more, you can only pass 1 to start with in the constructor
269+
if(typeof response.success === "string"){ alert(response.success); }
270+
else{ alert("Your submission has been received"); }
271+
},
269272
//validate the form, display any errors and return false to block submission
270273
validateForm: function(form){
271274
//add .was-validated for bootstrap to show errors
@@ -319,9 +322,12 @@ var remote_form = new FormFromURL('/my-form', {
319322
onload: function(form){ return this; }, //although you can add more, you can only pass 1 to start with in the constructor
320323
xhrSubmit: true,
321324
submitURL:null, //when null, the form's action will be used (if explicitly defined), otherwise it falls back to the URL the form was retrieved from
322-
submitMethod:null, //when null, the form's method will be used (if explicitly defined), otherwise it falls back to POST
323-
onError: function(error, response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
324-
onSuccess: function(response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
325+
submitMethod:null, //when null, the form's method will be used (if explicitly defined), otherwise it falls back to POST
326+
onError: function(error, response, form){ alert(error); }, //although you can add more, you can only pass 1 to start with in the constructor
327+
onSuccess: function(response, form){ //although you can add more, you can only pass 1 to start with in the constructor
328+
if(typeof response.success === "string"){ alert(response.success); }
329+
else{ alert("Your submission has been received"); }
330+
},
325331
//validate the form, display any errors and return false to block submission
326332
validateForm: function(form){
327333
//add .was-validated for bootstrap to show errors
@@ -341,27 +347,32 @@ var remote_form = new FormFromURL('/my-form', {
341347
remote_form.getForm();
342348
```
343349
344-
<h4 id="6lineofjs">How to get and submit a form in 6 lines of javascript:</h4>
350+
<h4 id="3lineofjs">How to get and submit a form in 3 lines of javascript:</h4>
345351
346-
Your javascript
352+
Your javascript (success/failure messages will be shown in an alert)
347353
```javascript
348-
var email_form = new FormFromURL('/email-form', {
354+
new FormFromURL('/email-form', {
349355
insertIntoElement: 'body.form-container',
350-
onError: function(error){ alert(error); },
351-
onSuccess: function(response, form){ alert(response.success); }
352-
});
353-
email_form.getForm();
356+
}).getForm();
357+
```
358+
Your HTML
359+
```html
360+
<div class="form-container"></div>
354361
```
355362
Server response when retrieving /email-form
356363
```json
357364
{
358-
"html": "<form><input name='email' required='required'></form>"
365+
"html": "<form><input name='email' required='required'><input type='submit' value='Submit'></form>"
359366
}
360367
```
361-
Server response when submitting to /email-form
368+
Server response when submitting to /email-form (success)
362369
```json
363370
{ "success": "Thank you for submitting, your email has been provided to spammers everywhere" }
364371
```
372+
Server response when submitting to /email-form (error)
373+
```json
374+
{ "error": "Your email is required/Your email is invalid" }
375+
```
365376
366377
#### Extending:
367378

0 commit comments

Comments
 (0)