JavaScript function unable to execute form action properly

I have a link RESET YEAR which triggers a servlet to check if the current year is equal to the present year. If they are not equal, then the function resetyear() is supposed to be called. The issue I am facing is that the function is not working as expected. Despite assigning an action to the form, it fails to submit and only displays alert till "hello". The flow of my function seems to get stuck at this point.

I would appreciate any suggestions or recommendations you might have.

Here are the functions in question:

 function resetyear(){

if(confirm("DO YOU WANT TO RESET THE YEAR?"))
{
    alert("hello");
    var formname = document.getElementById("indexform");
    alert(formname);
    document.forms[0].action = "resetmaster";
    alert("hi")
    
    document.forms[0].submit();  
    alert("over");
    
    alert(1);
}
else{

    alert("nothing");
}
}

  function dontreset()
{
alert("YOU CAN'T RESET THE YEAR");
}

and here is the HTML CODE:


<html>
<head>
    <title>CHANDNA COLDSTORAGE</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="js/calculateamt.js" type="text/javascript"></script>
    <link href="CSS/style.css" rel="stylesheet" type="text/css">
    <script>
        function MM_goToURL() { //v3.0
            var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
            for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
        }
    </script>

</head>

<% 
String role = "";
role = (String) session.getAttribute("role");

String flag = "";
flag = (String) session.getAttribute("flag");
System.out.println("flag = " + flag);
if (flag == null) {
    flag = "";
}
if (flag.equals("yes")) {
%>   
<script>

   alert(1);
  // resetyear();
    dontreset();
    //document.getElementById("checkyear").value = "1";
    //alert(document.getElementById("checkyear").value);

</script>
<%} else if(flag.equals("no"))
    {%>
<script>
    alert(2);
    //document.getElementById("checkyear").value = "2";
    //alert(document.getElementById("checkyear").value);
    resetyear();
</script>
<%}else{}%>
<body> 
    <form name="indexform" method="post" id="indexform">
        <div id="maindiv">
            <div align="center">
                <p> </p>
                <p id="Title"> CHANDNA COLD STORAGE </p>

                <input type="hidden" name="checkyear" id="checkyear" value="">
            </div>

            <table width="459" border="0" align="center">

                <%if (role != null && role.equals("admin")) {%>
                <tr>
                    <td height="70"><p align="center"><a href="showoctdet.jsp">OCCUPANT'S LIST</a></p></td>
                </tr>
                <tr>
                    <td height="70"><p align="center"><a href="/ColdStorage/regisbean">REGISTRATION PAGE</a></p></td>
                </tr>
                <tr>
                    <td height="70"><p align="center"><a href="Acceptanceform.jsp">ACCEPTANCE PAGE</a></p></td>
                </tr>
                <tr>
                    <td height="70"><p align="center"><a href="CashReciept.jsp">CASH RECIEPT</a></p></td>
                </tr>
                <tr>
                    <td height="70"><p align="center"><a href="Report.jsp">REPORT</a></p></td>
                </tr>
                <tr>
                    <td height="70"><p align="center"><a href="DetailsTrack.jsp">TRACK DETAILS</a></p></td>
                </tr>
                <tr>
                    <td height="70"><p align="center"><a href="/ColdStorage/resetyear">RESET YEAR</a></p></td>
                </tr>

                <%} else {%>
                <tr>
                    <td height="70"><p align="center"><a href="Report.jsp">REPORT</a></p></td>
                </tr>

                <%}%>



            </table>
            <p> </p>
            <p align="center">
                <input type="submit" name="signout" id="signout" value="SIGN OUT" onClick="MM_goToURL('parent','Login.jsp');return document.MM_returnValue">
            </p>
    </form>

</body>
</html>

Answer №1

Check out this quick demonstration of all the alerts:

JSFiddle Link

If you're experiencing issues, it could be related to caching. Try debugging your JavaScript code in Chrome or Firefox to identify any potential problems.

As I've included a JSFiddle link, here is the corresponding code snippet:

<body>
    <script>
        function resetyear() {

            if (confirm("DO YOU WANT TO RESET THE YEAR?")) {
                alert("hello");
                var formname = document.getElementById("indexform");
                alert(formname);
                document.forms[0].action = "resetmaster";
                alert("hi");
                //alert(document.forms['indexform'].action);
                document.forms[0].submit();
                alert("over");
                //form.action = "/resetmaster";
                //form.submit();
                alert(1);
            } else {

                alert("nothing");
            }
        }
    </script>
    <form name="indexform" method="post" id="indexform">
        <div id="maindiv">
            <div align="center">
                <p>&nbsp;</p>
                <p id="Title">CHANDNA COLD STORAGE</p>
                <input type="hidden" name="checkyear" id="checkyear" value="">
            </div>
            <table width="459" border="0" align="center">
                <%if (role !=n ull && role.equals( "admin")) {%>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="showoctdet.jsp">OCCUPANT'S LIST</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="/ColdStorage/regisbean">REGISTRATION PAGE</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="Acceptanceform.jsp">ACCEPTANCE PAGE</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="CashReciept.jsp">CASH RECEIPT</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="Report.jsp">REPORT</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="DetailsTrack.jsp">TRACK DETAILS</a>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td height="70">
                            <p align="center"><a href="javascript:resetyear()">RESET YEAR</a>
                            </p>
                        </td>
                    </tr>
                        <%} else {%>
                            <tr>
                                <td height="70">
                                    <p align="center"><a href="Report.jsp">REPORT</a>
                                    </p>
                                </td>
                            </tr>
                        <%}%>
            </table>
            <p>&nbsp;</p>
            <p align="center">
                <input type="submit" name="signout" id="signout" value="SIGN OUT" onClick="MM_goToURL('parent','Login.jsp');return document.MM_returnValue">
            </p>
    </form>
</body>

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

obtaining values from a JSON object and JSON array in java

I am struggling to generate a JSON string that combines both a JSON object and a JSON array. Here is the desired format: { "scode" : "62573000", "sname" : "Burn of right", "icd10" = [ {"icode" : "T25.229?", "iname" : "Right foot"}, {"icode" ...

The JQuery script is not producing any results

After integrating a script into my website template, it is not functioning as expected. I suspect there may be a conflict with JavaScript, but upon inspecting with firebug, I am unable to identify any abnormalities. Here is the link for reference: Link ...

Ruby on Rails slider bar functionality

Currently, I am developing a video-focused application using Ruby 1.9.2 and Rails 3.1. One of the key features I need to implement is a slider bar that allows users to adjust the total duration of a video in real-time. Despite my attempts to use HTML5 fo ...

A successful AJAX request in Spring MVC is authenticated

I am facing some issues while trying to send a jQuery AJAX request to my Spring MVC Servlet. I have read several articles, but none of them have been able to help me with my problem :( Here is the AJAX request code snippet in question: $.ajax( ...

WebPack Error: When calling __webpack_modules__[moduleId], a TypeError occurs, indicating that it is not a function during development. In production, an Invalid hook call error

Encountering a WebPack error when utilizing my custom library hosted as a package and streamed with NPM Link. Interestingly, the production version functions flawlessly. Below are my scripts: "scripts": { "dev": "rm -rf build ...

Issue with improper lighting on Three.Geometry objects when using LambertMaterial in Three.js

After enhancing my initial version of this inquiry (previously asked question) with a JSFiddle showcasing the latest build of Three.JS and illustrating the lighting issue on Three.Geometry, I encountered difficulties with dependencies in Stack Snippets. Ho ...

D3 group of legendary elements

Is there a way to group both the circle and text elements for each legend item together? I'm currently facing a challenge with the enter/exit methods. While I can create the groups (g), I'm unable to append the text and circle elements. li.sel ...

Disable the ng-change event

Is there a way to prevent the ng-change event from happening? I have a select box with an option that I don't want users to select. Instead, I want to display a warning message and then revert back to the previously selected option. To achieve this, ...

What is the best way to iterate through JavaScript objects within a Jade template?

Technologies such as Node.js, Jade, socket.io, jQuery, and JSON are being used in my project. In my "index.jade" file, I have the following code that receives "results" data as JSON from the backend: extends layout block content ul // more jade html h ...

Issue with the datepicker not toggling properly in Angular-UI 0.11.0, only opens

I am struggling with implementing 2 datepickers using Angular UI version 0.11.0. Here is my HTML code: <span ng-if="periods.period == 10"> <input type="text" datepicker-popup="dd-MMMM-yyyy" ng-model="cdate.customStartDate" is-open="opened1" ...

Enhance your security with Ember-simple-auth when using ember-cli-simple-auth-token

Currently, I am in the process of utilizing ember-simple-auth alongside ember-cli-simple-auth-token: "ember-cli-simple-auth-token": "^0.7.3", "ember-simple-auth": "1.0.1" Below are my configurations: ENV['simple-auth-token'] = { authoriz ...

What could be causing the continuous occurrence of the error message stating "Module 'socket.io' cannot be found"?

For the past few days, I've been attempting to incorporate socket.io into my NodeJS script. However, every time I run it, I encounter the frustrating error message stating "Cannot find module 'socket.io'". The detailed error is as follows: ...

Transferring data from a callback function within a component to a different component

I am currently utilizing the giphy-api module in Node.js and I need to transfer this data to React. Within my setup, I have two key components: The first component, Api.js, effectively connects with Node.js and returns a callback containing all of the AP ...

Is there a way to deactivate the toggle button in my code?

<label class="switch switch-yes-no" id="disable_'+id+'" style="margin: 0;font-weight: bold;"> <input class="switch-input" type="checkbox" id="disable_'+id+'" /> <span class="switch-label" data-on="Enabled" data-off="Disab ...

JavaScript was unable to locate the requested URL on the server

After successfully deploying and accessing the URL using Firebase's hosting feature, everything seems to work fine. However, when I try to access a specific endpoint like this: https://*******.web.app/api/send, I encounter the following error message: ...

Incorporating Products from an Iframe into the Cart on Woocommerce

I recently set up a store using WooCommerce, and I have a unique situation where my customizable products are displayed within an iframe. The "add to cart" button is also contained in this iframe. Whenever I click on the Add to Cart button, I receive a mes ...

Enhancing interactivity: Implementing a ripple effect for Card clicks in Material UI

I'm curious if there's a method to incorporate a ripple effect into Material UI's Card component when it is clicked. Additionally, I am wondering if it is achievable to have the ripple effect display on top of the content within the Card co ...

Issue with pushing inner list<object> in Knockout version 3.2.0

Currently, I am working with knockout.js on an ASP application. The issue I am facing involves a list of objects returned by the controller action to the view, where the objects in the list point to another list of objects. My struggle lies in adding new o ...

Exploring end-to-end testing with NestJS and Guards

I'm trying to test an endpoint called /users using nestjs, but I encountered some errors. I'm unsure how to fix the issues and make the test pass with a guard. First Issue Nest is unable to resolve dependencies of the UserModel (?). Please en ...

Is there a way to programmatically update a webpage using Javascript?

I have been attempting to set up the code in a way that the page automatically changes using setTimeout. However, I am unable to make it work. setTimeout()(page3, 500); function page3() { changepage3('automatic') } Even though my code is str ...