Michele Riva Michele Riva - 16 days ago 12
Ajax Question

Prevent form submission with Semantic-Ui



I'm working with Semantic Ui Forms but I can't figure out how to prevent form submission after form validation.

I can't find anything in their documentation… thanks a lot.

This is my current submission script:

$(function () {
$('#formId').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'phpScriptUrl.php',
data: $('form').serialize(),
success: function () {
//"ok" label on success.
$('#successLabel').css("display", "block");
}
});
});
});

Answer

Using callbacks. I added some form validation as example

$('.ui.form').form({
    fields: {
      firstname     : 'empty',
      lastname   : 'empty'
    },
    onSuccess: function(event, fields) {
        event.preventDefault();
            $.ajax({
        type: 'post',
        url: 'phpScriptUrl.php',
        data: $('form').serialize(),
        success: function () {
          //"ok" label on success.
          $('#successLabel').css("display", "block");
        }
      });      
    }
  })
;