HTML Quiz Form Data Submission via Email

I recently designed a quiz using HTML that consists of 5 questions. I have programmed it in a way that only one question appears at a time, keeping the upcoming questions hidden from users taking the quiz. However, since each question is within a separate form, sending all the answers via email has become tedious as only the last question's answer gets sent. Is there a method to combine all the forms into one or maybe include a button to submit all the forms together?

Any assistance on this matter would be highly appreciated :)

Unfortunately, I am unable to share the code at the moment. I will attempt again later.

Answer №1

Greetings and a warm welcome to Stackoverflow!

If my understanding is correct, you have the option to divide the form into sections as shown below:

<form POST="..." onsubmit="onNextStep">
   <div class='form-section' id='question-1'>
      <!-- code here -->
   </div>
   <div class='form-section' id='question-2'>
      <!-- code here -->
   </div>
   <div class='form-section' id='question-3'>
      <!-- code here -->
   </div>
   <div class="form-footer">
      <button type="submit"> Next </button>
   </div>
</form>

Afterwards, you can implement an event on button click that increments the current step (0, 1, 2...) and hides all the divs without the correct id. Finally, submit the form when reaching the final step.

let totalSteps = 3;
let step = 1;

function onNextStep(e) {
  const currentStep = step;
  const nextStep = step + 1; 

  // If it's the last step, change the button text
  if ( nextStep == totalSteps ) {
    document.querySelector('form button[type="submit"]').innerText = "Submit"
  }

  // Once the last step is completed, submit the form.
  if ( nextStep > totalSteps ) {
    return
  }
 
  // Avoiding form submission
  e.preventDefault()

  // Hide the current section
  document.getElementById('question-'+nextStep).style.display = 'block'
  
  // Display the next section
  document.getElementById('question-'+currentStep).style.display = 'none'  

  // Update the steps counter
  step += 1
}

With some tweaks, this example should suit your requirements perfectly.

Answer №2

Only utilize a single form for your questions.

Wrap your questions in the fieldset tag and then use JavaScript to toggle visibility as required.

let questions = document.querySelectorAll(".question");
let answerButtons = document.querySelectorAll(".question button");

for (var i = 0; i < answerButtons.length; i++) {
  answerButtons[i].addEventListener("click",function() {
    //Find the closest question
    var question = this.closest(".question");   
    //Remove Active class from current question
    question.classList.remove("active");
    //Add active class to the next sibling
    question.nextElementSibling.classList.add("active");
  });
}
.question:not(.active) {
  display: none;
}
<form action="">
  <fieldset class="question active">
    <legend>Question 1 of 3</legend>
    <label>What is your name? <input type="text" name="qName"></label>
    <button type="button">Submit Answer</button>
  </fieldset>
  <fieldset class="question">
    <legend>Question 2 of 3</legend>
    <label>What is your quest? <input type="text" name="qQuest"></label>
    <button type="button">Submit Answer</button>
  </fieldset>
  <fieldset class="question">
    <legend>Question 3 of 3</legend>
    <label>What is your quest? <input type="text" name="qName"></label>
    <button type="submit">Submit Quiz</button>
  </fieldset>
</form>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending a blob through AJAX to a different domain using CORS

Could someone please explain why my current request is being blocked by the SO policy restriction? Javascript Code: var blob = new Blob([req.response], {type: "application/octet-stream"}); req = new XMLHttpRequest(); req.open("POST", ws_path(other_contex ...

I'm still searching for a proper solution on how to access JavaScript/jQuery functions within Colorbox

For my website, I am utilizing PHP, jQuery/JavaScript, Colorbox (a jQuery lightbox plugin), Smarty, and other tools. Currently, I am working on displaying data in a popup using the Colorbox plugin. However, I am facing an issue with calling a JavaScript fu ...

What is the most effective way to display a card with varying values depending on the user's input in a form?

For a while now, I've been grappling with a particular challenge. In my project, I am utilizing multiple states to display values within a card after they are entered into a form. The first state captures the values and modifies the initial state, whi ...

How can I get the bootstrap grid to span across multiple rows?

I'm trying to create a collage of images using Bootstrap 5, but I can't figure out how to make elements span multiple rows. Here's the layout I want to achieve: https://i.sstatic.net/JjnF0.png (guess I don't have a good reputation yet ...

Steps to obtain the precise source code of a webpage

Is there a way to download the exact source code of a webpage? I have tried using the URL method and Jsoup method, but I am not getting the precise data as seen in the actual source code. For example: <input type="image" name="ctl00$dtlAlbums$ct ...

Tips for identifying text within HTML code

Looking for help with this HTML code: <span class="navbar-text navbar-nav company-title">aLine</span> The text "aLine" is displayed on the navigation bar. Can you guide me on how to locate this text using xpath? ...

What is the proper method for activating the lights in a traffic light sequence?

I've been working on creating a traffic light sequence where each time the "change lights" button is pressed, the light is supposed to change. However, I'm facing an issue where the code only displays the red light and doesn't switch to ambe ...

Could you provide guidance on how to toggle the visibility of a div using the click event of a button or link located on a different HTML page in Angular?

Is there a way to change the visibility of a div on a different page when a button or link is clicked? For example, I have a button on the "main.html" page that, when clicked, should display a hidden div on the "header.html" page. How can I achieve this? ...

Is there a way to prevent Bootstrap-4 drop-down from automatically applying certain styles?

When creating my new website, I decided to use Bootstrap-4. However, I ran into an issue with the drop-down feature. While the drop-down works perfectly fine in the header nav-bar, it seems to be adding some unwanted inline styles when used within the page ...

Enhancing data entry by using a dropdown menu to update the record value without adding any undefined data elements

Currently, I am working on enhancing a Location edit form that includes an API-fed dropdown list of Departments. The task involves utilizing the record's departmentId to preselect the current value in the dropdown menu. However, a complication arises ...

Steps to customize a CSS file within node_modules

Is there a way to make changes to a CSS file in the "node_modules" dependency without them being overwritten when I run npm install? I want to keep the modifications I've made to the node module files. ...

javascript the confirm alert function is not functioning properly when paired with a disabled button in an ASP.NET WebForms application

My webform has a submit button that triggers a javascript function. Here is the code for the button: <asp:Button ID="btnSub" runat="server" Text="submit" OnClientClick="return processSub();" /> The javascript function being called looks like this: ...

Why can't we import Angular 4 as a library similar to AngularJS?

Why was AngularJS introduced as a script to import in an HTML page? However, in the newer version Angular 4, we need to use a web server to launch the application? Is it because AngularJS is not considered a framework but Angular 4 is? Thank you. ...

Troubleshooting: Unusual Page Loaded on Webpack Dev Server

While working with web pack, I encountered an issue when trying to run the npm run build:dev script. Instead of seeing the actual website, I was presented with a strange page displaying my workspace folders: https://i.sstatic.net/mBNKg.png In case it&apos ...

Sending parameters in GraphQL with Typescript results in an empty set of curly braces being returned

I am new to learning GraphQL with Typescript and I am trying to pass an argument in a GraphQL function to return something dynamically. I have been struggling with this issue for the past hour and could not find any solutions. Here are the relevant code sn ...

The width values in the array are constantly shifting upon refreshing

Would it be possible to create an array called slide_widths, which will contain the widths of all the .slide elements? $( document ).ready(function() { var $slider = $('.slider ul'); var slider_width = $slider.width(); var $slides = $(&apo ...

Inject Custom ASP Control Script into the DOM dynamically upon registration

During a postback, I am loading my ascx control when a dropdown change event occurs. Parent C#: private void ddlChange() { MyControl myCtr = (CallScript)Page.LoadControl("~/Controls/MyControl.ascx"); myCtr.property = "something"; // setting publ ...

Utilizing JavaScript on webpages within a Next.js/React component

In an attempt to replicate a project similar to the following: https://codepen.io/andytran/pen/GpyKLM It is evident that Javascript plays a crucial role in the functionality of the page. The goal is to develop a custom Next/React component utilizing this ...

Finding the Ideal Location for Controllers in an Express.js Project

I'm relatively new to software development and one concept that I find challenging is organizing the directory structure of various projects. As I prepare to embark on an Express project, I prefer keeping controller classes separate from route callbac ...

How to effectively compare time duration with a timer in the Laravel framework

I am managing a table that stores various job entries for employees. Each job entry includes a column for duration. I would like to trigger an alert or other event when the duration of a job has ended. My project is built using Laravel and VueJS. Below i ...