am-impact / amforms

Forms plugin for Craft
Other
122 stars 21 forks source link

Ajax Sending #144

Open waswebb opened 7 years ago

waswebb commented 7 years ago

Hi

First i want to say that this plugin is a very cool one. I like your work, thx.

But it would be nice to have the option to send the form over Ajax. Do you plan to add this option?

hubertprein commented 7 years ago

We are planning to add this option in time.

CreateSean commented 6 years ago

+1 would love to see this added.

thisisjamessmith commented 6 years ago

It seems to handle ajax submissions just fine for me? Am I missing something here?

Moorst commented 6 years ago

@thisisjamessmith are you just submitting to 'amForms/submissions/saveSubmission' via ajax?

The following doesn't work for me, getting 404;

var xhr = new XMLHttpRequest();
xhr.open('POST', 'amForms/submissions/saveSubmission', true);
xhr.setRequestHeader('Accept', 'application/json; charset=utf-8');
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

// Send the collected data as JSON
xhr.send(JSON.stringify(formData));
thisisjamessmith commented 6 years ago

No, I'm submitting to the form's action attribute (which is actually empty, so defaults to posting to the current page).

I'm using jQuery - something like this:

ajaxSubmit: function($form, $thanks, e){
  e.preventDefault();

  $.ajax({
    type:'POST',
    url: $form.attr('action'),
    data: $form.serialize(),
    cache: false
  }).done(function(data){
    //success...
    if (data.success){
      // remove loading and show success message
    }
    // errors...
    else{
      if (data.errors){
        $.each(data.errors, function(field, msgs){
          // show errors by each errored field
        });
      }
    }
  });
},
Moorst commented 6 years ago

Ahh ok great, I'll give that a go. Thanks!

ipetrov87 commented 6 years ago

@thisisjamessmith You save my day! Thank you!