Formdata failed to pass button value to servlet

I have a scenario where I need to select one button out of multiple options (similar to radio buttons). However, when I try to retrieve the selected button's value in my servlet, it returns null. Below is the code I am using.

JS PAGE

$.each($('.radio-btn'), function(key, value) {
  $(this).click(function(e) {
    $('.radio-btn-selected').removeClass('radio-btn-selected').addClass('radio-btn');
    $(this).removeClass('radio-btn').addClass('radio-btn-selected');

    // do whatever you want on click
  });
});

CSS PAGE

.radio-btn {
  background-color: #FFFFFF;
  border: 1px solid #4A4A4A;
  color: #4A5362;
  font-size: 14px;
  line-height: 26px;
  outline: none;
  padding: 7px;
  cursor: pointer;
}

.radio-btn-selected {
  background-color: #FFFFFF;
  border: 1px solid #55BC7E;
  color: #55BC7E;
  font-size: 14px;
  line-height: 26px;
  outline: none;
  padding: 7px;
  cursor: no-drop;
}

JSP PAGE

<form action="<%=request.getContextPath() %>/BookAppointment" method="POST">
  <div class="timmings">
    <div id="ajaxResponse" style="display:none">
      <div class="radio-btn-row">
        <div class="radio-btn-wrapper">
          <button id="bt1" class="radio-btn" name="slot" type="button" value="10:00 AM">10:00 AM</button>
        </div>
        <div class="radio-btn-wrapper">
          <button id="btn2" class="radio-btn" name="slot" type="button" value="10:30 AM">10:30 AM</button>
        </div>
        <div class="radio-btn-wrapper">
          <button id="btn3" class="radio-btn" name="slot" type="button" value="3:00 PM">03:00 PM</button>
        </div>
        <div class="radio-btn-wrapper">
          <button id="btn4" class="radio-btn" name="slot" type="button" value="3:30 PM">03:30 PM</button>
        </div>
      </div>
    </div>
    <div class="book" style="display:none"><input type="submit" class="submit" value="Book Appointment"></div>
  </div>
</form>

Servlet Page

package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/BookAppointment")
public class BookAppointment extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public BookAppointment() {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date=request.getParameter("date");
        String slot=request.getParameter("slot");
        System.out.println(date+" "+slot1);
    }
}

Any suggestions would be appreciated.

Answer №1

It appears that you are expecting the value of the 'selected' button to be transmitted during form submission - however, buttons do not function in this manner. To rectify this issue, adjustments will need to be made either in your JS or HTML code.

One solution would involve adding a hidden field that is updated when a button is selected. It is worth noting that the $.each() loop is unnecessary in your jQuery code; simply apply click() to all elements within the jQuery object.

$('.radio-btn').click(function(e) {
  $('.radio-btn-selected').removeClass('radio-btn-selected').addClass('radio-btn');
  $(this).removeClass('radio-btn').addClass('radio-btn-selected');

  $('#slot').val(this.value);
});
.radio-btn-row {
  display: flex;
  flex-direction: row;
  margin-top: 20px;
}

.radio-btn-wrapper {
  margin: 0px 4px;
}

.radio-btn {
  background-color: #FFFFFF;
  border: 1px solid #4A4A4A;
  color: #4A5362;
  font-size: 14px;
  line-height: 26px;
  outline: none;
  padding: 7px;
  cursor: pointer;
}

.radio-btn-selected {
  background-color: #FFFFFF;
  border: 1px solid #55BC7E;
  color: #55BC7E;
  font-size: 14px;
  line-height: 26px;
  outline: none;
  padding: 7px;
  cursor: no-drop;
}

.radio-btn-disabled {
  cursor: no-drop;
}

.submit {
  margin-top: 10px;
  padding: 5px;
  background-color: #33b5e5;
  border: 2px solid black;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="<%=request.getContextPath() %>/BookAppointment" method="POST">
  <div class="timmings">
    <div id="ajaxResponse">
      <!-- style="display:none"> -->
      <div class="radio-btn-row">
        <div class="radio-btn-wrapper">
          <button id="bt1" class="radio-btn" name="slot-button" type="button" value="10:00 AM">10:00 AM</button>
        </div>
        <div class="radio-btn-wrapper">
          <button id="btn2" class="radio-btn" name="slot-button" type="button" value="10:30 AM">10:30 AM</button>
        </div>
        <div class="radio-btn-wrapper">
          <button id="btn3" class="radio-btn" name="slot-button" type="button" value="3:00 PM">03:00 PM</button>
        </div>
        <div class="radio-btn-wrapper">
          <button id="btn4" class="radio-btn" name="slot-button" type="button" value="3:30 PM">03:30 PM</button>
        </div>
      </div>
    </div>
    <div class="book" style="display:none"><input type="submit" class="submit" value="Book Appointment"></div>
  </div>

  <input type="text" id="slot" name="slot" />
  <!-- make this hidden in your production version -->
</form>

Alternatively, you could modify your HTML by replacing button elements with actual radio inputs. Unlike buttons, radio inputs send values in the request automatically. This adjustment eliminates the need for JS to manipulate classes as styling can be solely controlled through CSS.

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

"Resolving conflicts: Dealing with jQuery's noConflict and undefined

I've been struggling with getting this to work and have searched through various Q&A's about jQuery's noConflict() without finding a solution that fits my specific circumstances. If this question has already been answered elsewhere, I apolog ...

Using the combination of Spring Boot and Thymeleaf, implement server-side validation within a modal form

Currently, I am delving into spring boot and thymeleaf, and one thing that confuses me is how to perform validation within a modal form without redirecting. My Class Model public class Class { @Id @GeneratedValue(strategy = GenerationType.IDENT ...

Organizing Content with Bootstrap - Utilizing Sections and Divs

Currently utilizing a Bootstrap template for my website development, I am encountering two challenges that have me stumped: 1) Upon placing my HTML code within a <section>, an automatic top margin/padding seems to be applied to the section. Despite ...

Contrasting the use of jQuery versus AJAX for updating static page text

While I haven't fully grasped the concept of AJAX yet, my understanding is that it can be beneficial for updating specific parts of a webpage. Does using AJAX only make sense when you require server interaction? I am looking to replace text on a webp ...

Ways to showcase Switch/Case in two separate drop-down menus

I'm currently working on a PHP file with a switch/case structure that I believe is in JSON format. While I may not be an expert in PHP, AJAX, and JSON, I'm really putting effort to learn more about it. <?php switch($_GET['make'] ...

Incorporating Bootstrap Content: A Guide to Including a PHP File within Another PHP File

I am encountering an issue when trying to import a PHP file containing my navigation bar into another PHP file. Here's the code for the navigation file: <?php echo " <html> <head> <nav class = "navbar navbar-default navbar-fixed-top ...

"What is the best way to send back multiple responses to an AJAX request in a Struts action class

Looking for some guidance on handling responses in an Ajax call from a Struts (1.3.10) action class. I've been using the PrintWriter class to send responses back to the client's browser, and while it works sometimes, there are instances where the ...

The example in the MUI x-data-grid documentation always seems to fall short of expectations

I followed the code from the material-ui library documentation located at this link: https://mui.com/x/react-data-grid/layout/#flex-layout My aim is to set up a flex layout on my project, I went through the steps mentioned in the documentation but keep ...

Transferring data - the dynamic duo of Ajax and PHP

I have a basic search form that I would like to use to display results in a DIV, so I've implemented ajax for this purpose. <script type="text/javascript> $(document).ready(function(){ $('#boton_cargar').click(function() { ...

Enhancing the readability of modals with Angular Dialog Service

I have been utilizing the Angular Dialog Service to create popup forms on my website. The source code for this service can be accessed here: https://github.com/m-e-conroy/angular-dialog-service/blob/master/README.md However, I am experiencing an issue wit ...

the new adjustment has not taken effect on index.php

I have been working on my index.php file and have included various other files as well. Let me show you the structure: <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css" /> <style type= ...

Why doesn't the WordPress Genesis child-theme inherit the parent CSS styles?

I am new to using the Genesis framework and I find myself confused about its child themes. The Genesis theme comes with its own stylesheet, but in order to use Genesis, you need to install a child theme. However, the child themes I have come across (such ...

Function that is triggered by scrollbar height indicator

I'm currently utilizing jQuery to retrieve the user's current height and triggering an animation function once they reach that height (similar to reactive websites where animations occur as the user scrolls down the page). However, I am struggli ...

Steps for choosing the nth HTML row with jQuery

I'm facing a situation where I need to be able to select the nth row of an HTML table based solely on the id of the selected row. You can see exactly what I mean by checking out this JSFiddle Demo <table class="mytable1"> <tr><td i ...

Ways to modify HTML tag content using Java

Is there a way to update HTML content within a tag using Java? Take the following example: Before: <html> <head> </head> <body> <div>text<div>**text**</div>text</div> </body> & ...

Display one div and conceal all others

Currently, I am implementing a toggle effect using fadeIn and fadeOut methods upon clicking a button. The JavaScript function I have created for this purpose is as follows: function showHide(divId){ if (document.getElementById(divID).style.display == ...

Tips for hiding a div only on mobile devices while always displaying it on large screens using AngularJS or CSS

How can I use Angularjs or css to hide a div on mobile devices only, ensuring that it is always displayed on large screens? ...

Locking Bootstrap 4.3 Navbar at the Page's Summit with a See-Through Effect

I have extensively researched and searched online for information before reaching out here. I hope my question is not redundant. My challenge lies in creating a static navbar at the top of my Bootstrap 4.3 page. Despite multiple attempts, it seems to elud ...

The Jquery ajax function fails to execute after upgrading the framework version from 3.5 to 4.0

After testing with alert, I am facing an issue where the ajax call is not working in my aspx.cs file when switching from .NET framework 3.5 to 4.0. The ajax function being used is: function GetCustomers(pageIndex) { $.ajax({ t ...

Rails encountered a syntax error while attempting to parse JSON data, due to an unexpected character found at line 2, column 1

I'm a beginner when it comes to using AJAX in Ruby on Rails. What I'm trying to achieve is that when a user clicks on a link with the class .link (redirecting to an external site, for example www.google.de), it should send data - specifically the ...