When the ajax response comes in, my javascript code seems to suddenly stop

After sending a POST request, my JavaScript suddenly stops working for some unknown reason.

Here's the situation: I visit my webpage, fill out the form, and then click 'Click me' :

Upon clicking OK in the alert popup, I see the expected outcome:

However, when I try to click the button again after filling out the form, nothing happens! Even the alert popup doesn't show up.

Below are the files I am using:

Starting with my CSS file, simple2.css :

    body, html {
        margin:0;
        padding:0;
        height:100%;
    }

    a {
        font-size:1.25em;
    }

    #content {
        padding:25px;
    }

    #fade {
        display:none;
        position:absolute;
        top:0%;
        left:0%;
        width:100%;
        height:100%;
        background-color:#ababab;
        z-index:1001;
        -moz-opacity:0.8;
        opacity:.70;
        filter:alpha(opacity=80);
    }

    #modal {
        display:none;
        position:absolute;
        top:45%;
        left:45%;
        width:64px;
        height:64px;
        padding:30px 15px 0px;
        border:3px solid #ababab;
        box-shadow:1px 1px 10px #ababab;
        border-radius:20px;
        background-color:white;
        z-index:1002;
        text-align:center;
        overflow:auto;
    }

    #results {
        font-size:1.25em;
        color:red;
    }

Next is my HTML file, simple2.html :

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <link rel="stylesheet" type="text/css" href="simple2.css">
            <title>simple II</title>
    </head>
    <body>
            <div id="fade"></div>
            <div id="modal"> <img id="loader" src="images/loading.gif" /> </div>
            <div id="results"><!-- Results are displayed here -->
                    <form method="post" name="start" target="_blank">
                            <p>Enter thing1: <input type="text" id="thing1" name="thing1" size="10" /></p>
                            <p>Enter thing2: <input type="text" id="thing2" name="thing2" size="10" /></p>
                            <p>Enter thing3: <input type="text" id="thing3" name="thing3" size="10" /></p>
                            <p>Check thing4: <input type="checkbox" id="thing4" name="thing4" value=1>
                            <input type="hidden" id="state" name="state" value="one" /></p>
                    </form>
                    <button id='clickme' name='clickme'>Click me</button>
                    <script src="simple2.js?0000000000015"></script>
            </div>
    </body>
    </html>

Then comes my JavaScript code in simple2.js :

    function openModal() {
            document.getElementById('modal').style.display = 'block';
            document.getElementById('fade').style.display = 'block';
    }

    // More JavaScript functions...

Lastly, my CGI script, simple2.py :

    #!/usr/bin/env python

    import cgi
    import os
    import cgitb; cgitb.enable()
    import time
    import calendar

    // Python CGI script functions...

Answer №1

document.getElementById("results").addEventListener("click", function(e) {
        if(e.target && e.target.nodeName == "BUTTON") {
            // Adjusted Prevent Default placement to allow
            // other events to bubble up.
            e.preventDefault();
            // We have detected a Button click
            var inputs = document.querySelectorAll("input");
            var params = inputs[0].name + "=" + inputs[0].value;
            for( var i = 1; i < inputs.length; i++ ) {
                    var input       = inputs[i];
                    var name        = input.name;
                    var value       = input.value;

                    params +=       "&" + name + "=" + value;
            }
            alert( params );
            loadAjax( "/cgi-bin/simple2.py", encodeURI(params));
       }
});

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

What is the best method for embedding JSON data into a script tag within a Zope Chameleon template?

Creating a pyramid/python application where the view callable passes in an array variable called data, structured as [[[x1,y1,z1,],...],[[v1,v2,v3],...]] In the view callable: import json jsdata = json.dumps(data) I aim to place this data within a java ...

Manipulate paths in JS/JQuery by dynamically adding parameters with an indefinite number of values

My JavaScript script relies on an AJAX call that adds a parameter to an HTML request, but I've encountered two issues. I'm struggling to find a straightforward method to retrieve the pathname with parameters. With pure JavaScript, location.path ...

How can I trigger a success event in JavaScript after ASP.NET form validation?

Is there a way for me to be notified when my ASP.NET form validation is successful so I can subscribe to that event? Here's the situation: When a user clicks a button on a payment form, I want to display a message saying "processing," but only if the ...

Is there a way to create a spinning slot machine animation using CSS3 and jQuery?

I'm currently working on a demo application that will randomly choose a venue when a user clicks a button. My goal is to have the selected venues scroll through with a slot machine spinning animation created using CSS3 and jQuery. Initially, I consid ...

The variable in the dataTables JavaScript is not receiving the latest updates

//update function $('#dataTable tbody').on('click', '.am-text-secondary', function() { //extract id from selected row var rowData = table.row($(this).parents('tr')).data(); var updateId = rowData.id; ...

Verify a roster within a display by utilizing annotations

My solution involves using a listbox to display a list as shown below, and I am also utilizing Chosen.js to allow for selecting multiple records. @Html.ListBox("AllLanguages", new SelectList(ViewBag.Languages, "Id", "Language1")) <div id="languagesDi ...

Creating a callback in C code with Emscripten for JavaScript integration

In this challenge, the goal is to incorporate a JavaScript function as a callback to display progress during a while-loop operation. For example: var my_js_fn = function(curstate, maxstate){//int variables console.log(curstate.toString() + " of " + maxsta ...

The React Native Android app encountered an error loading the JS bundle: updates made to index.android.js are not reflecting in the generated APK file

Setting up a react-native android project on my Windows machine has been successful so far. To run the project, I use the command react-native run-android and then the installed apk is generated on my phone. After making some changes in the index.android. ...

Establish a timeout period when using the hover function

My dynamically loaded content requires a specific method of invocation due to its unique nature. While the process runs smoothly without using a setTimeout, is there any way to introduce a 0.25 second delay in this scenario? Check out this fiddle for ref ...

Dealing with errors in Next.js when using axios in Express

Currently, I am working on implementing the login feature for my application using an asynchronous call to my API. The issue I am facing is that despite express throwing an error, the .then() function is still executing with the error instead of the actual ...

JavaScript drop-down menu malfunctioning

I am currently in the process of learning web development languages, and I'm attempting to create a dropdown list using JavaScript (I previously tried in CSS without success). I would greatly appreciate it if you could review my code and let me know ...

Is it possible for me to identify the quantity of children in CSS when the number of children surpasses four?

Is it necessary to style each child when the number of children exceeds four, within a parent element? I am aware of the nth-child(4) ~ child method, but this only targets the siblings that come after the fourth child. In this scenario, do I need to style ...

React textarea trigger function on blur event

https://codesandbox.io/s/react-textarea-callback-on-blur-yoh8n?file=/src/App.tsx When working with a textarea in React, I have two main objectives: To remove focus and reset certain states when the user presses "Escape" To trigger a callback function (sa ...

What are some effective ways to test asynchronous functions in React?

Looking for help to test async/Promise based methods in React components. Here is a simple example of a React component with async methods: import server from './server'; class Button extends Component { async handleClick() { if (await ca ...

Deliver the Express.js: transmit outcomes post next() invocation

I have a unique app feature that allows users to create their own routes in real-time. I also want to show a custom 404 message if the route is not found, so I added this code to my last middleware: app.use((req, res, next) => { // ...normal logic ...

Selecting CSS Properties with jQuery

I am trying to make an image change color to blue and also change the background color to blue when it is clicked inside a div. However, my code doesn't seem to be working as expected. Is there a more efficient way to apply CSS to elements? FIDDLE v ...

Utilizing various camera set-ups within Three.js

How can I smoothly switch between two different cameras in Three.js while transitioning from one to the other? Imagine a scene with a rock and a tree, each having its own dedicated camera setup. I'm looking for a way to seamlessly transition between ...

Typescript: Add a new variable preceding a specified string

fetchArticle(articleId: string): Observable<any> { return this._http.get(`${this._url}/${articleId}`) .map((response: Response) => response.json()) .do(value => console.log(value)) .catch((error) => Observable.throw(err ...

The transfer of JSON data to PHP through AJAX is not successful

I have encountered an issue while attempting to send a JSON string to a PHP file using ajax. The problem lies in the variable I am posting not being delivered when employing the JQUERY ajax method as illustrated below: <DOCTYPE html> <html> ...

Experiencing a problem with JQuery Offset when a fixed position is applied to a

My website has a page layout and style similar to the example provided in this JsFiddle. When I use JQuery to click on a button, the content is displayed correctly as shown below: However, if I scroll down the page first and then click the button, the co ...