A step-by-step guide on incorporating a div panel in HTML and JavaScript triggered by a submit button click within an HTML form

For the past 3 to 4 days, I've been grappling with an issue. I've put together a student registration form using HTML and JavaScript. The form includes validation through JavaScript and string concatenation to submit all form values and display submission via an alert. However, my dilemma lies in wanting to showcase all submission values in a div panel but lacking the expertise to do so. I'm reaching out for some guidance and assistance in modifying my JavaScript and HTML code to incorporate a div panel upon clicking the submit button. I'm attempting to follow the steps outlined on this site: W3schools link to create div panel. Could someone please lend a hand in integrating the W3schools code into mine?

This is my HTML Code:

<!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>Reg Form</title>
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
        <script type="text/javascript" src="reg.js"></script>
    </head>
    
    <body onload="document.registration.inputfirstname.focus();">
    
        <div class="container">
            <div class="row">
                <form class="form-horizontal" name="registration" onSubmit="return formValidation();">
                    <fieldset>
                        <legend>
                            <center>Registration</center>
                        </legend>
    
                        [...]
    
                        <div class="form-group">
                            <label class="col-md-4 control-label" for="submit"></label>
                            <div class="col-md-4">
                                <button id="submit" onclick="showme()" name="submit" class="btn btn-success">Submit</button>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </body>
    
    </html>

This is my JavaScript Code:

function formValidation() {
        [...]

    function showme() {
    
        var firstname = document.getElementById('inputfirstname');
        var lastname = document.getElementById('inputlastname');
        var rollno = document.getElementById('inputrollno');
        var Class = document.getElementById('inputclass');
        var email = document.getElementById('inputemail');
    
        var str = 'Hello ' + inputfirstname.value + inputlastname.value +', Your Roll no is' + inputrollno.value +',You have successfully registered for this course';
        alert(str); 
    }

I'd appreciate any help or suggestions in resolving this issue. Thank you!

Answer №1

Take a look at this piece of code

function validateForm() {
    var firstName = document.registration.firstName;
    var lastName = document.registration.lastName;
    var rollNo = document.registration.rollNo;
    var classInput = document.registration.classInput;
    var email = document.registration.email;

    if (validateFirstName(firstName, 5, 12)) {
        if (validateLastName(lastName, 6, 12)) {
            if (allNumeric(rollNo)) {
                if (isAlphanumeric(classInput)) {
                    if (validateEmail(email)) {
                        alert('Form Submitted Successfully');
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

function validateFirstName(firstName, minLen, maxLen) {
    var inputLen = firstName.value.length;
    if (inputLen == 0 || inputLen >= maxLen || inputLen < minLen) {
        alert("First Name should not be empty / length should be between " + minLen + " to " + maxLen);
        firstName.focus();
        return false;
    }
    return true;
}

// Repeat for other validation functions

var submitButton = document.getElementById('submit');
function showDetails() {

    var first = document.getElementById('firstName');
    var last = document.getElementById('lastName');
    var rn = document.getElementById('rollNo');
    var cl = document.getElementById('classInput');
    var em = document.getElementById('email');

    var details = 'Hello,' + firstName.value + ' ' + lastName.value + ', Your Roll no is' + rollNo.value + ',You have successfully registered for this course';
    
    document.getElementById('formData').innerHTML = 'First Name : ' + first.value + '<br>Last Name : ' + last.value + '<br>Roll No : ' + rn.value + '<br>Class : ' + cl.value + '<br>Email : ' + em.value ;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Registration Form</title>
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script type="text/javascript" src="reg.js"></script>
</head>

<body onload="document.registration.firstName.focus();">

    <div class="container">
        <div class="row">
            <form class="form-horizontal" name="registration" onSubmit="return validateForm();">
                <fieldset>
                    <legend>
                        <center>Registration Form</center>
                    </legend>

                    <div class="form-group">
                        <label class="col-md-4 control-label" for="firstName">First Name</label>
                        <div class="col-md-5">
                            <input type="text" name="firstname" id="firstName" placeholder="First Name" class="form-control input-md">

                        </div>
                    </div>

                    // Add HTML elements for Last Name, Roll No, Class, Email inputs

                    <div class="form-group">
                        <label class="col-md-4 control-label" for="submitButton"></label>
                        <div class="col-md-4">
                            <button id="submit" onclick="showme()" name="submit" class="btn btn-success">Submit</button>
                        </div>
                    </div>
                </fieldset>
            </form>
        </div>
        <div id="formData"></div>
    </div>
</body>

</html>

Answer №2

To easily display content, you can create a div with an id attribute like so:

<div id="formData"></div>

Then, whenever you need to show something:

var firstName = document.getElementById('inputFirstName');
document.getElementById('formData').innerText = 'First Name: ' + firstName.value;

Answer №3

One way to display student details is by creating a row with 5 cells inside it. You can then use the showme() function to populate these cells with values.


                       <div class="row" id="studentInfo" sytle="display: 
                         none;">
                        <div class="col-sm-2" id="firstName"></div>
                        <div class="col-sm-2" id="lastName"></div>
                        <div class="col-sm-2" id="rolNo"></div>
                          <div class="col-sm-2" id="class"></div>
                         <div class="col-sm-2" id="email"></div>

                       </div>

                function showme() {

                    var firstname = document.getElementById('inputfirstname');
                    var lastname = document.getElementById('inputlastname');
                    var rollno = document.getElementById('inputrollno');
                    var Class = document.getElementById('inputclass');
                    var email = document.getElementById('inputemail');

                 //assign input values to respective div elements in the row

                  document.getElementById("firstName").text(firstname.value);
                  document.getElementById("lastName").text(lastname.value);
                  document.getElementById("rolNo").text(rollno.value);
                  document.getElementById("class").text(Class.value);
                  document.getElementById("email").text(email.value);
                  //display the hidden div
                  document.getElementById("studentInfo").style.display = "block";
               }

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

Tips for choosing an option in a form using Selenium with JavaScript

I am currently in the process of automating some tests for a specific form, and I have successfully automated all steps except for selecting an option from a dropdown menu using JavaScript. Here is my code: const {Builder, By, Key} = require("sel ...

NextJS with the added option of customizable CSS styling

I'm having trouble getting my custom CSS library to work with my components in NextJS. I'm attempting to import my custom CSS file in my components, but it doesn't seem to be working. import React from 'react' import '../../s ...

Manipulating the DOM by linking to a div and using JSON information for opening and closing

I have retrieved data from a JSON file and displayed it as a link. I am looking to implement functionality that allows me to hide and show a div when the link is clicked. Below is the code I'm currently using: $(document).ready(function() { $ ...

Transferring data from JavaScript to PHP using the $.ajax method for storing information in a MySQL database

I am attempting to make a POST call from a JavaScript file to a PHP file in order to insert a variable into a MySQL database. Here are the basic files I am working with: 1° PHP file for sending the call <html> <head> <script ...

Placing a div over a JavaScript element

Is it feasible to overlay a div(1) with a background image on top of another div(2) that contains JavaScript (like Google maps API v3)? I have experimented with z-index without success, and I am unable to utilize absolute positioning because I depend on t ...

"Implementing a function to show input fields and mark them as mandatory when a specific option is

Here is an example form that I use: <form id="clac" action="#" method="post"> <select onchange="func_type()" name="t_person" id="t_person" class="t_person_class"> <option value="0" selected>select persons</option> <optio ...

Naming a JSON object twice

As a newcomer to node.js, I find myself struggling with understanding the process. Can someone please guide me on where I might be going wrong? This is how I create a new Product exports.postAddProduct = (req, res, next) => { const product = new Produ ...

Loading a div using Ajax within a frame

My php page includes a div that is supposed to be populated by an Ajax call. function showcopay() { var apa = document.getElementById("alert_id").value; $("#copay").load('show_copay.php?pid='+apa); } The parent page of the div used to be ...

Streamlining the Process of Uploading Files using Selenium WebDriver

My current project involves automating the uploading of an HTML file. Unfortunately, the website I am working on has made this task more complicated than necessary. The code for the file upload section is as follows: <div class="form-row"> < ...

In search of a customized CSS design for drop-down lists and menus inspired by Apple's sleek aesthetic

I've been on the hunt for a CSS example that showcases Apple-style drop down lists and buttons. Despite my efforts to search through Google, I can't seem to find exactly what I'm looking for. I've tried various search phrases like "Appl ...

Excessive screen height causes the CSS box layout to overflow

I am looking to create a screen layout similar to the one shown in this image. It should consist of a row with no overflow and include: A left box that contains: A fixed height header (20px), An aspect square box taking up the remaining height. ...

Retrieving the Windows user's username via Typescript or Javascript is an essential

How can I retrieve the current Windows username in my Angular 4 application using TypeScript or JavaScript? I am specifically working with the Chrome browser. ...

I am looking to transmit a JWT token to my backend using next-auth

In my current project using Next.js, I have implemented authentication with next-auth. This project follows the MERN stack architecture. I am facing an issue where I need to retrieve the JWT token and send it to my backend server using next-auth along wit ...

Guide to manipulating DOM elements with Angular.js: inserting or deleting elements using createElement

Within my Angular directive, I am dynamically generating an external script from the DOM for a specific object within a list. This script includes both a 'script' tag and div content. While I am able to successfully add the script, I am encounter ...

Saving words inside a text box using a variable

I am attempting to save a text value in an input element. The element is accessed through a jquery selector, where the variable selectedindex holds an integer from a dropdown list (can be 0, 1, or 2). Below is the code with both HTML and JavaScript. <l ...

One controller displays ng-repeats while the other does not

I have 2 controllers loading in different locations within my view. One controller works perfectly, but the other one does not show ng-repeats or appear in ng-inspector. I have confirmed that the http data is visible in the inspector. Both controllers are ...

What is the best way to display the user login in the center of a page with all necessary controls

Challenge How can the user login be displayed in the center of the page? Additional Information The current layout displays user login data on the left-hand side. I am looking to have the user login data centralized on the page, which includes labels f ...

Change the default values for grid column configurations in Ext JS globally

The Ext.grid.column.Column class contains the following configurations: draggable (Default: true) sortable (Default: true) menuDisabled (Default: false) Is there a way to globally change the default values of these configurations for all grid columns i ...

Enhance Your jQuery Skills by Adding Custom Directories to Anchor Links

Can jQuery be used to add a custom folder name in front of all links on a webpage? For example, if the website has these links: <a href="/user/login">Login</a> <a href="/user/register">Register</a> <a href="/user/forum">Foru ...

When accessing files.fileName.path in formidable, it may result in returning undefined

The server is unable to save the file. The path property is giving back an undefined value. const saveImage = (req, res) => { const form = formidable.IncomingForm(); form.uploadDir = `./images`; form.keepExtensions = true; form.parse(req, (er ...