Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit a form in Semantic UI?

I know how to validate a form using Semantic UI, and can even read in console the message "Form has no validation errors, submitting." However, where is this submitting to? I want to actually submit the form, but the way Semantic UI is laid out I don't seem to be able to specify where to submit to or anything.

I read this tutorial, but that uses Angular for submission and not just Semantic UI.

Am I missing something really simple here?

like image 313
Jon Carl Avatar asked Oct 20 '13 20:10

Jon Carl


People also ask

Which is better semantic UI or material UI?

According to the StackShare community, Material-UI has a broader approval, being mentioned in 67 company stacks & 77 developers stacks; compared to Semantic UI, which is listed in 77 company stacks and 50 developer stacks.

Is Semantic UI good?

Semantic UI React is great overall if you are looking for UI library that can help your React applications with consistent styles. It is beginners-friendly without a long learning curve. Semantic UI lets developers to build websites with fast and concise HTML, along with a complete mobile responsive experience.

Is Semantic UI free?

Semantic UI is a free open source project already used in multiple large scale production environments.


1 Answers

You can use jQuery's ajax:

   //Get value from an input field
   function getFieldValue(fieldId) { 
      // 'get field' is part of Semantics form behavior API
      return $('.ui.form').form('get field', fieldId).val();
   }

   function submitForm() {
      var formData = {
          field1: getFieldValue('someId')
      };

      $.ajax({ type: 'POST', url: '/api/someRestEndpoint', data: formData, success: onFormSubmitted });
   }

   // Handle post response
   function onFormSubmitted(response) {
        // Do something with response ...
   }

EDIT: also, you can use the onSuccess method of the form to run the submitForm function, ie when you initialize the form:

$('.ui.form').form(validationRules, { onSuccess: submitForm });

onSuccess will only be called when the 'Submit' button is clicked and the form is valid based on the rules you specify.

EDIT: If you want the regular HTML form behavior, you will need to add the semantic css classes to the form tag.

<form class="ui form" method="POST" action="/signup">...</form>

And then you set up the validation rules using jQuery. This will give you the default HTML form behavior, ie when you hit the submit button, it will make a POST request to /signup in the case above. If any of your rules trigger, the submit is prevented until there is no validation errors.

like image 144
Agon Bina Avatar answered Oct 02 '22 17:10

Agon Bina