0

I am developing an app and I am submitting multiple forms using only one button.

I have given them the same class and iterated through them and then submitted it. In my localhost, it works but in heroku it sometimes works and sometimes it submits only a couple or just one.

   const submitBtn = document.getElementById('submit-form');
   const forms = document.querySelectorAll('.form');

   submitBtn.addEventListener("click", () => {
     forms.forEach(form => form.submit());
   }

Isaac
  • 1

1 Answers1

0

Can I see your html as well?

My gut says the page might be refreshing before the second form submits? By default, form.submit() will redirect to the URL in the action attribute of the form.

Possible duplicate of Submit two forms with one button ?

Looks like calling successive form.submit() functions is pretty unreliable. I would check the answers in the linked post and see if they work for you. Maybe combine the form data and make one request, or do them one at a time and wait for the response before submitting the next one.

georgedum
  • 491
  • 3
  • 10
  • 1
    It dont think it refreshes cause sometimes submits the first and the last form, sometimes it submits all of them and sometimes only the last one. <% @user_reports.each_with_index do |report, i| %> <%= simple_form_for(@feedback, url: user_report_feedbacks_path(report), class:"feedback-form") do |f| %> <%= f.input :feedback_score, collection: [ 0, 5, 10, 15, 20]%> <% end %> <% end %> – Isaac Jun 04 '20 at 17:23
  • Hmm hard to know for sure, but it looks like there are a lot of issues handling forms this way. Maybe one of the solutions in the linked post will help. Good luck! – georgedum Jun 04 '20 at 17:54