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

Retrieve tag, custom taxonomy, and attachment information using the get_pages function

Currently, I have implemented code that retrieves all pages submitted by the currently logged-in author using the get_pages function. The retrieved result is then passed to Javascript via Ajax. Everything functions as expected, and the correct pages are be ...

Using CSS to customize the appearance of the preview in CKEditor

After successfully integrating CKEditor into my website CMS, I am facing an issue with the Preview button not using stylesheets (CSS). Despite editing the preview.html file located in: Website/ckeditor/plugins/preview/ It appears that the CSS is being ig ...

JavaScript's ASYNC forEach function not following the expected sequence

I'm really struggling to understand the workings of async and await in this scenario. I want the forEach function to run before the console.log and res.json, but no matter what I do with async and await, it always ends up being the last thing executed ...

Tips on invoking a function from an array in JavaScript when a button is clicked

Being new to JavaScript, I encountered a challenge where I have an array of functions: allFunctions = () => [ function1(), function2(), function3(), function4(), function5(), function6(), function7(), function8(), function9() ] My go ...

The split function of a string displays an undefined result

My goal is to extract all characters that come after the equal sign within a URL: let url = this.$route.query.item console.log(typeof(url)) // outputs string let status = url => url.split('=')[1] When I run the code, it shows &apo ...

Meteor, enhanced with the dynamic Iron Router module and integrated

I am working on a project using Meteor (Meteor.com) and I want to incorporate iron-router for the page routing along with the pre-existing accounts-ui package for login functionality. Previously, my {{loginButtons}} functioned properly, but ever since I i ...

Unsupported Media Type: 415 Error during MultipartFile Upload

When trying to upload a file using spring boot and vue, I encountered an error '415 : Unsupported MediaType'. This is the snippet of my spring boot controller. @PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, produces = {MediaType. ...

Using es6 map to deconstruct an array inside an object and returning it

I am looking to optimize my code by returning a deconstructed array that only contains individual elements instead of nested arrays. const data = [ { title: 'amsterdam', components: [ { id: 1, name: 'yanick&a ...

React: Updating State with the Spread Operator and Array Method to Add Elements to an Array

Link to my jsfiddle code snippet: https://jsfiddle.net/ilikeflex/r2uws1ez/30/ I've been attempting to update the state in React by accessing the previous state. There are three different cases where I'm trying to achieve this, but I'm havin ...

What is the procedure for altering a particular element using ajax technology?

I have an AJAX request that updates the user information. I need to retrieve a specific value from the response and update the content of a specific element. For example, here is the element that needs to be changed: <div id="changeMe"><!-- New ...

Is it possible to retrieve the ID of an anchor tag when the text within two table items meets my specified criteria?

In the table with ID "PTSRCHRESULTS," each row contains anchor links. I am trying to find the ID of an element within a row that contains both "Undergrad" and "1". This is for Power Automate Desktop, but I can use JavaScript to get the necessary ID to clic ...

Receive a notification when the div element stops scrolling

I am attempting to replicate Android's expandable toolbar within an Angular component. My HTML code appears as follows: <div (scroll)="someScroll($event)"> <div class="toolbar"></div> <div class="body"></div> </d ...

Do we really need to implement ajax in this code snippet?

In my scenario, I have two JSP files named test1.jsp and test2.jsp. The flow of my program goes like this: I need to retrieve data from a textbox in test1.jsp, but the Ajax call is initiated from a different page. My objective is to receive the controller ...

Vue js lacks the ability to effectively link HTML elements to corresponding JavaScript functions

I seem to be missing a crucial element in my spring boot application. I am attempting to follow the initial steps outlined in the Vue documentation to create the most basic Vue application possible. Here is what I currently have: @Controller public class ...

Tips for properly invoking a function from one component to another component

After browsing through a few questions on the topic of parent/child elements, I have come across a particular node tree that looks like this: IndexPage -> Modals -> ClientDetails (it's modal component) -> Header My goal is to ...

Streaming live audio through socket io. Need help troubleshooting ALSA shutdown issue

Looking to develop a live audio streaming service using socket.io and ionic 4. On the client side, utilizing cordova-plugin-audioinput and ng-socket-io for Angular. For the server, employing standard npm packages. Node version: 10.16.0 ...

AngularJS Issue: Duplicate Error

Attempting to refresh data after making some changes results in duplicate errors appearing within the ng-repeat. The scenario is as follows; I am working on creating a dynamic menu module The requirements include adding, deleting, changing the order, an ...

Using JavaScript, you can manipulate the position of a line on a canvas to

I want to create a feature where users can manipulate lines on a canvas by skewing them. This means they would be able to drag one end point of the line to a desired point on the same x-axis using JavaScript and HTML5 canvas. Can someone please provide g ...

Using an array to dynamically input latitude and longitude into a weather API call in PHP

I am currently working on a leaflet map project that showcases the top 10 cities in a selected country. The markers are added dynamically based on the coordinates provided in the $latLng array. For example, when Australia is chosen from the select field, t ...

Tips for storing the output of a script URL in a JavaScript variable

I am currently facing an issue with running a script in the 'head' element of a webpage. The script makes an API call to a specific URL and retrieves results in the form of a JavaScript array. However, I am encountering difficulty because the API ...