question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple forms on the same page

See original GitHub issue

Description:

Hi,

I try to have multiples forms on the same page (one for each language). However, when I try to get data, it’s always the data from the last form.

Thanks.

Environment Details:

  • formBuilder Version: 3.4.2
  • Browser: Chrome 83
  • OS: MacOS

Expected Behavior

Data of all forms

Actual Behavior

I get data of last form instead of data of each form.

Example

https://jsfiddle.net/fdehanne/1j9c8fry/14/

<button id="save">Save</button>
<div class="render"></div>
<div id="formEditor-fr_FR" data-culture="fr_FR" class="form-editor"></div>
<div id="formEditor-en_GB" data-culture="en_GB" class="form-editor"></div>
var formsBuilder = [];

$(function() {
  $('.form-editor').each(function() {
    var culture = $(this).attr('data-culture');
    formsBuilder[culture] = $(this).formBuilder();
  });

  $(document).on('click', '#save', function() {
    $('.render').html('');
    for (var culture in formsBuilder)
    {
      $('.render').append(culture + '<br>');
      var data = formsBuilder[culture].formData;
      $('.render').append(data + '<br>');
    }
  });
});

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
myowintheincommented, Jun 24, 2020

Sure, here is the sample of my usage.

Initiate Multiple Instances

let formIDs = ['#form1', '#form2']
let fbInstances = []
let options = [............]

let init = function(i) {
    if (i < formIDs.length) {
        options['formData'] = [] // add existing form components here (for rendering)
        $(formIDs[i]).formBuilder(options).promise.then(res => {
            fbInstances.push(res)
            i++
            init(i)
        })
    }
}

init(0)

Without recursive function, all previous form instance will be override by later one. In above case, outputting of form1 will always show as form2.

Get Data from All Instances on Submit

const formData = []

fbInstances.forEach(fbInstance => {
    formData.push(fbInstance.actions.getData())
})

Never use these methods for getting data. Even if you initiate form builder with recursive function, only the components of last builder will return. Use this instead.

0reactions
maximus1127commented, Jul 30, 2021

when I do this, I get Cannot read property 'then' of undefined. the first form id in #form1 gets rendered, but the second one never does. The way I solved this was to render like normal:

let fbInstances = []
        let intakeForm = $('#formBuilderIntake').formRender({
            formData: //my formdata here
        });

fbInstances['intake'] = intakeForm

Then later down the page:

let symptomForm = $('#formBuilderSymptom').formRender({
            formData: //my formdata again
        });
fbInstances['symptom'] = symptomForm

Now the user data can be accessed like this: form1responses = fbInstances['intake']['userData']

I hope this helps someone in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to place two forms on the same page? - Stack Overflow
You could make two forms with 2 different actions <form action="login.php" method="post"> <input type="text" name="user"> <input ...
Read more >
Multiple forms on one page: bad practice? - Dev Talk
Hi there,. I received a design guide, where 3 different forms are placed on the same page. All of them have about 6–8...
Read more >
can I add multiple form to one single html page - MSDN
Yes, an html page can have multiple forms. The forms cannot be nested though. ... Of course, it can contain many forms in...
Read more >
Multiple forms on the same page - WordPress.org
I use multiple forms [shortcodes] on the same page. I have one in the footer loaded on every pages, and on one particular...
Read more >
Include Multiple Forms on a Page - Intermediate Django
Include Multiple Different Forms on the Same Page · Step 1: Create the Forms · Step 2: Include the Forms in a View...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found