Learn the method of implementing a hidden attribute in the DOM through Servlets

Is there a way to set the hidden attribute in the DOM using servlet doPost? The HTML is generated by the servlet as shown below:

out.println("<html>");
out.println("<head>");
out.println("<title>Make payment</title>");
out.println("<script type='text/javascript' src='js/jquery-1.5.2.min.js'></script>");
out.println("<script type='text/javascript' src='js/payment.js'></script>");
out.println("<link type='text/css' href='css/style.css' rel='Stylesheet' />");
out.println("</head>");
out.println("<body>");
out.println("<div class='bg-light' style='width: 200px; height: 200px; position: absolute; left:50%; top:50%;  margin:-100px 0 0 -100px; padding-top: 40px; padding-left: 10px;'>");
out.println("<input id='reservationID' style='display: none' value='"+rb.reservationID+"' />");
out.println("<div>Credit Card Number : </div>");
out.println("<div><input id='creditcard' onKeyPress='return checkIt(event);' type='text' name='creditcard' maxlength='16' /></div>");
out.println("<div>ExpirationDate : </div>");
out.println("<div><input id='expirationDate' type='text' onKeyPress='return checkIt(event);' name='expirationDate' maxlength='4' /></div>");
out.println("<span style='font-size: 75%;'>"+Error+"</span>");
out.println("<div><input type='button'  name='buttonsave' value='Make Payment' onclick='makePayment("+rb.reservationID+");' /></div>");
out.println("<div><input type='button'  name='buttoncancel' value='Cancel Payment' onclick='cancelPayment("+rb.reservationID+");' /></div>");
out.println("</div>");
out.println("</body>");
out.println("</html>");

An HTTP servlet request is used to retrieve values from the creditcard and expirationDate fields. These values are then compared to the database to set CCA.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
    creditno = request.getParameter("creditcard");
    expiration = request.getParameter("expirationDate");

    if (request.getParameter("buttonsave") != null) {
        UserBean us = new UserBean();
        boolean check = us.checkCC(userID, request.getParameter("creditcard"), request.getParameter("expirationDate"));
        if (check == true) {
            CCA = 1;
        } else {
            CCA = 0;
        }
    }

Now, I need to access these variables from JavaScript in payment.js to display an alert. How can I achieve this? Can I write them to a hidden field in the DOM?

Thank you in advance!

Answer №1

Are you looking for something along the same lines?

console.log("FormName value: " + FormName);

Within the submitForm function,

var HiddenValue = document.getElementById('HiddenInput').value;

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

Using Javascript to Create Interactive Visualizations of Fourier Transform

Utilizing this particular library for performing a Fast Fourier Transform on an audio file, my next step involves visualizing the output using canvasjs. Unfortunately, I am unsure about how to proceed with this visualization. I find myself puzzled regardi ...

Encountered an issue when attempting to create meanjs using yo

After installing yeoman globally, I encountered an error while trying to generate meanjs with yo. Unfortunately, I was unable to identify the root cause of the problem. view image here Any help would be greatly appreciated. I attempted using sudo as well ...

Guide on positioning the camera to track the model in a third-person view

Currently, I am tackling a project in Three.js where I am in need of setting up a third-person camera that tracks a 3D model as it navigates through the scene. Despite having a basic framework with animations and controls, I am encountering difficulties in ...

The HTML button triggers a function to execute on a different webpage when clicked

I'm facing a straightforward issue that I can't seem to figure out due to my limited experience with Angular and web development. The problem revolves around two components, namely home and dashboard. In the home.component.html file, there's ...

At what point do static fields get initialized in Java?

Within this simple Singleton implementation, at what point does the new Singleton1() initialization take place (indicated with an asterisk *)? public final class Singleton1 { private Singleton1() { } private static Singleton1 instance = new Si ...

Troubleshooting problems with AngularJS loading data through ajax

One of the custom widgets in my application relies on Angular functionality. On a particular page, this widget is loaded via ajax. The following content is fetched through ajax and inserted into the DOM: _abc.html: <script type="text/javascript">c ...

What is the best way to display or conceal buttons when the textarea is in focus or out of focus, while still allowing them

Seeking help to include two buttons beneath the input box. One button is for saving the input in the textarea, while the other is for aborting. The buttons should only be visible when the input field is focused. An issue I am facing is that clicking on ...

`Unable to retrieve Shopify products using shopify-api-node`

I'm encountering a 403 error while trying to retrieve products from my custom Shopify app using the shopify-api-node module. Below is the code snippet I've put together based on a helpful discussion on Stackoverflow: const express = require(& ...

javascript issue with Q promise not passing complete parameters

I am struggling to pass all three arguments successfully in my promise callback. The issue I'm facing is that only one argument is being received instead of the expected three: var asyncFunction= function(resolve) { setTimeout(function() { ...

Tips for accessing the initial element of an array, storing it in a state, and sending it to a component

Is there a way to access and store the first object from the response data in a state? I attempted to achieve this by utilizing setData and assigning the first object of the data to it: export default function BuildingsDetail({buildingId}) { const [data ...

The lightbox feature on the page is not functioning properly

On my website, I have a beautiful lightbox created using fancybox.net. You can check it out here: https://i.sstatic.net/R0OUM.jpg I also use gallery codes to display images in a gallery format. Below is the jQuery code I am using: $(document).ready(fun ...

What steps can I take to resolve my password validation rule when confirming during sign-up?

Utilizing react-hook-form in combination with Material-UI for my sign-up form has been a challenge. I am currently working on implementing a second password field to confirm and validate that the user accurately entered their password in the initial field, ...

An alternative approach to dynamically adding a class to a DOM element using AngularJS that reduces repetition

Creating an AngularJS application with materializecss for the UI involves adding the focused class to all form elements for specific behavior. To streamline this process, you can use the ng-class directive on the parent container and define a variable tha ...

Is it possible to dynamically assign a template reference variable to a newly created DOM element in TypeScript?

After creating a DOM element with this.document.createElement('h1'), I am looking to insert the element into a section tag using a template reference variable (myTRF), similar to the example below: <section> <h1 #myTRF>This is my he ...

Issues with processing the $ajax URL in Ajax and jQuery calls are causing malfunctions

Allow me to provide a clearer explanation. My goal is to dynamically update the content of an HTML element based on data received from a REST API. The process involves making an AJAX call to the API URL and then updating the content accordingly. <butt ...

Using Selenium web drivers across various computers and directories

Working on a collaborative project with others involving automated tests using Selenium. Utilizing Git for code sharing, but facing the issue of having to constantly update driver paths after each check-in due to local storage on individual computers. The ...

Using Dynamic Jinja HTML to Select Elements Dynamically

I have a unique system where forms are dynamically created based on values from a dictionary. Each form is assigned an ID corresponding to the key value and initially hidden. <div id="subforms" style="display: none" > {%for k, v in options.items() ...

When initializing the JFrame, it will first display the element at index [1] of the array within the for loop instead of the element

My attempt at creating an application to loop through a string array as a beginner has hit a snag. The goal was to display numbers one through five on a JFrame using JLabel, with each number changing every second. However, when the JFrame appears, it show ...

Refreshing select2 dropdown options dynamically with AJAX updates

I have a select dropdown for locations that is initialized using select2 on page load. I am looking to dynamically update the data in the dropdown at regular intervals using ajax calls. However, when I attempt to update the data in select2, the dropdown ...

How to use jQuery to determine if an image has finished loading

I'm facing an issue with the code below, it's not functioning properly: var timeoutID=0; var currentImage=0;//first image is position 0 in arrImages array var arrImages=[bla bla bla array of image URLs]; function slideShow() { if($('#my ...