Example: Building Simple Form

Problem: We want to generate a complex form and save its result.

Step 1: Preparing Data

Use the Population data structure from last lesson and add fields such as "value," to write through a POST request. Here's how it should look:

Step 2: Adding a New Web Component in the Plugin

Next, go to your plugin and add a new "web component" with two parameters: an endpoint for reading data and a title to display on your simple form.

Add Initialization Script:

function(instance, context) {

  div = $('<form id="myForm"></form>');


Add "Update" Script:

function(instance, properties, context) {  
var ctx = document.getElementById('myForm');
var layout = $('myForm') // get form by id
var data = context.endpoint || [] 
var title = context.title
// reading data by api endpoint for set your future select
var data = context.endpoint || []
// replace below URL to your api-endpoint for saving data
var endpointForSave = "https://api.directual.com/good/api/v5/data/population/getPopulation?appID=2...b65&sessionID="

//build you form
$form = $("#myForm");
$form.empty() //remove all inputs
$form.append('<h1>' + title + '</h1>');
$form.append('<input type="text" value="you name" name="name">');
$form.append('<select id="population" name="value"></select>');
  $('#population').append('<option value="' + datum.value + '">' + datum.value + '</option>');
var send = $("<button>Send data to server</button>");
  //prepare json object to send server
  var formdata = $form.serializeArray();
  var data = {};
  $(formdata).each(function(index, obj){
      data[obj.name] = obj.value;
  $form.append('<h1>' + title + '</h1>');
  $form.append('<span>sending data: ' + JSON.stringify(data) + ' to server</span>');
  fetch(endpointForSave, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    body: JSON.stringify(data)
    $form.append('<h1>' + title + '</h1>');
    $form.append('<span>Congratulations, the data has been sent to the server</span>');


Final Step: Using Your Plugin in Web Pages

Insert the step with your component into the page and set it up.

Open your page, fill out the form, and send it to the Directual platform.

Last updated