Using the EDAPI script step with an HTML form: The form
In the previous post, #EDAPI script step's new actions (for JS in FM) we started to explore the idea of using forms to create records. Let’s take that further in this post by discussing the HTML form and how it is used.
In HTML we can create a form. At its basic structure, we have a <form> element and various <input>s for data entry. Here’s an example of a basic basic form:
<form id="form">
<input type="text" />
<input type="button" name="submit-button" value="Submit"/>
</form>Here’s a more complex example, one with CSS styling (using Tailwind CSS).
<form class="bg-white shadow-sm ring-1 ring-gray-900/5 sm:rounded-xl md:col-span-2">
<div class="px-4 py-6 sm:p-8">
<div class="grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div class="sm:col-span-6 ">
<label for="CompanyName" class="block text-sm font-medium leading-6 text-gray-900">Company name</label>
<div class="mt-2">
<input type="text" name="CompanyName" id="CompanyName" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"/>
</div>
</div>
<div class="col-span-full">
<label for="StreetAddress" class="block text-sm font-medium leading-6 text-gray-900">Street address</label>
<div class="mt-2">
<input type="text" name="StreetAddress" id="StreetAddress" autocomplete="StreetAddress" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"/>
</div>
</div>
<div class="sm:col-span-2 sm:col-start-1">
<label for="City" class="block text-sm font-medium leading-6 text-gray-900">City</label>
<div class="mt-2">
<input type="text" name="City" id="City" autocomplete="address-level2" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"/>
</div>
</div>
<div class="sm:col-span-2">
<label for="region" class="block text-sm font-medium leading-6 text-gray-900">State</label>
<div class="mt-2">
<input type="State" name="State" id="State" autocomplete="address-level1" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"/>
</div>
</div>
<div class="sm:col-span-2">
<label for="Zip" class="block text-sm font-medium leading-6 text-gray-900">ZIP</label>
<div class="mt-2">
<input type="text" name="Zip" id="Zip" autocomplete="Zip" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"/>
</div>
</div>
<div class="sm:col-span-2">
<label for="Zip" class="block text-sm font-medium leading-6 text-gray-900">Phone (xxx-xxx-xxxx)</label>
<div class="mt-2">
<input type="tel" name="Phone" id="Phone"pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" placeholder="XXX-XXX-XXXX" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"/>
</div>
</div>
</div>
</div>
<div class="flex items-center justify-end gap-x-6 border-t border-gray-900/10 px-4 py-4 sm:px-8">
<button type="button" class="text-sm font-semibold leading-6 text-gray-900">Cancel</button>
<button type="submit" class="rounded-md bg-purple-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-green-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-green-600">Save</button>
</div>
</form>IN the simple example, one of the <input> items is a button that will eventually handle the submitting of the form. In my example, I have a <button> with a type of “submit”.
When a user clicks on the button (in either instance) the form element is designed to perform an action.
There are a few ways to add the submit action to a form. Here’s one I wrote in my JS.
const data = {};
const form = document.querySelector("form");
form.addEventListener("submit", (e) => {
e.preventDefault();
const inputs = form.querySelectorAll("input");
inputs.forEach((input) => {
data[input.name] = input.value;
});
console.log(data);
const edapiObj = {
layouts: "customers",
action: "create",
fieldData: data,
};
FileMaker.PerformScript("edapi - record", JSON.stringify(edapiObj));
});This code will find the form element and ‘addEventListener’ to it. Adding the type ‘submit’ to it with a function will ensure that the form runs this function.
Inside this submit function is a lot of code, but it basically gathers all the values from the various inputs along with each input’s name and stuffs it into an object inside the variable ‘data’.
inputs.forEach((input) => {
data[input.name] = input.value;
});And then, once the data is collected, the data can be sent to FileMaker.
const edapiObj = {
layouts: "customers",
action: "create",
fieldData: data,
};
FileMaker.PerformScript("edapi - record", JSON.stringify(edapiObj));Notice in this I’m creating an object called ‘edapiObject’ and including three things: layouts, action, and fieldData. Not so coincidentally, these are the three items the Execute Data API script step needs in order to do its job.
The form itself is rather simple and can be constructed in two ways: either hardcode it in the HTML or use some JS to build it. Both are workable solutions, and both have drawbacks.
When we hard-code it, we are essentially building this one form for one table’s data. It is explicit and directly tied to a table.
If we were to use JS to create the form, we could have more flexibility in the form: passing the field names of a given table to the JS could construct the form on the fly. We could use one code base for all forms.
We’ll talk more about forms in this week’s #Office Hours. So please register and let me know you’re attending.
In tomorrow’s post, we’ll explore exactly what the eDAPI script step needs, and look at the script itself.