Executing an AJAX request in the onsubmit event of a form results in the form submission proceeding even when return false is

I am facing an issue with a form that I intend to use only for an AJAX call. The AJAX call is triggered on submit in order to utilize the auto-check feature for required fields. Despite returning false on submit, the form still submits. Surprisingly, when I comment out the statement responsible for the AJAX call, the form does not submit. Below is the code snippet of the form I am using. Any ideas why I can't prevent form submission?

<form action="" method="post" onsubmit="ajaxReq (); return false;">
    <input type="hidden" name="type" value='something'>
    <input type="text" name="testText" required>
    <input type="submit" value="AJAX_TEST">
</form>

Below is the ajaxReq function:

//I will be using the data from the form inside data as the data of the ajax call
//Currently data in the form is ignored
function ajaxReq() {
    var ajaxSettings = {
        'url' : '',
        'async' : false,
        'type' : 'POST',
        'data' : {
            'type' : 'SOME_TYPE'
        },
        'success' : function(data) {
            data = JSON.parse(data);

            //emptying this part did not change the result
            //will be using data here

        }
    };

    $.ajax(ajaxSettings);

    return false;

}

Answer №1

To assign an identifier to your form, such as id="myform", follow these steps:

$('#myform').on('submit', function(event) {
  event.preventDefault();
  initiateAjaxRequest();
});

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

Explore the input field to find relevant information, triggering an Ajax PHP call that displays search results in a convenient dropdown

Include an input box that displays a dropdown list of customer names when text is typed by the user <script> function displayCustomerList() { $.ajax({ url: "customerlist.php", success: function(result) { ...

Can AdonisJS 4.1.0 support conditional queries?

I am exploring the capabilities of AdonisJs query builder by referring to their documentation at Currently, I am attempting to replicate a scenario similar to the one demonstrated under the 'Conditional Queries' section: const query = Database. ...

Angular single page application with a sleek, user-friendly navigation bar for easy browsing

I have been attempting to solve the issue at hand without much success. As a newcomer to web development, I have been working on creating a basic app using angularjs and bootstrap. My pages are fairly sparse, consisting only of a few inputs and buttons t ...

HTML Scripts: Enhancing Web Functionality

I have another question regarding executing a script (docs()) upon clicking on text. I attempted to use the following code: <p><span class="skill">Documentation can be found here.<script>docs()</script></span></p> Unfo ...

Unable to update values in Google Sheets using the node.js API

I have been working on a node.js project that involves extracting the location of a cell based on a person's name and date. While I am able to determine this information easily, I encounter difficulties when trying to update the cell using the .update ...

The use of multiple Where clauses in a Firestore Firebase query is not functioning as expected when implemented in JavaScript code

https://i.stack.imgur.com/DdUGj.png db.collection('User_Info').where("User_Name", "==", "Sam").where("PASSWORD", "==", "c2FtMTIzQA==").get().then(snapshot => { if(snapshot.docs.length > 0 ){ debugger; alert("Login Successful."); ...

Styling buttons with different colors using React onClick event.getSelectedItem() method

I am having an issue with adding a border when the class is set to "transportation active" in my React code. When I click on one of the icons, all of them become active instead of just the clicked one. This behavior is not what I want, as I only want the ...

Tips for showing form data upon click without refreshing the webpage

Whenever I input data into the form and click on the calculate button, the result does not appear in the salary slip box at the bottom of the form. However, if I refresh the page and then click on the calculate button, the results are displayed correctly ...

Tips for combining values with Reactive Forms

Is there a way to merge two values into a single label using Reactive Forms without utilizing ngModel binding? <label id="identificationCode" name="identificationCode" formControlName="lb ...

Issue Encountered While Deploying Next JS Application Utilizing Dynamic Routing

I just finished developing my Personal Blog app with Next JS, but I keep encountering an error related to Dynamic Routing whenever I run npm run-script build. Below is the code for the Dynamic Route Page: import cateogaryPage from '../../styles/cards ...

Is the JavaScript code not executing properly?

There seems to be an issue with this code as it's not displaying an unordered list of HTML elements as intended. I've written the script and HTML code using Sublime Text, but even after trying to run the script in Chrome's developer console ...

The offspring of a React component

How can I select a specific div in children using React without passing it as a prop? I want to transform the code snippet from: <Pane label="Tab 1"> <div>This is my tab 1 contents!</div> </Pane> to: <Pane> <div&g ...

Validation of VAT numbers using JavaScript instead of PHP

I came across an interesting function at this link that is used for checking VAT numbers in the EU. However, I am struggling to integrate it with my registration form. I would like to convert it into a JavaScript function so that I can validate the number ...

What is the method for activating a hook after a state change in a functional component?

I'm currently working on a custom pagination hook that interacts with an API to fetch data similar to React Query. The concept is straightforward. I aim to invoke this hook whenever a specific state (referred to as cursor) undergoes a change. Below i ...

Guide: Building Angular/Bootstrap button checkboxes within a loop

I am in the process of designing a grid (table with ng-repeat) in which each row contains 4 columns of buttons. My goal is to use checkboxes as the buttons, like the Angular/Bootstrap btn-checkbox, so that they can be toggled on and off. I plan to set thei ...

What is the best way to create a line break in a flex div container to ensure that overflowing items wrap onto the next line using

Using material-ui popper to display a list of avatars. Trying to arrange the avatars horizontally within the popper. <Popper style={{ display: 'flex', maxWidth: '200px', }}> <div style={{ marginRight: '20px' }}&g ...

The lack of intellisense in VS Code for Node.js poses a significant challenge for developers

I am facing a problem with IntelliSense in Visual Studio Code. Despite installing multiple extensions, it is not working as expected. The IntelliSense feature only shows functions and variables that have already been used in my code file. Below is the li ...

Guide to adding checkbox IDs and inserting them into a textarea

Hello, my name is Prasath and I am currently learning about AJAX. I am working on a project involving seat layouts and finding it challenging to retrieve both the seat fare and seat number simultaneously. Is there anyone who can assist me in resolving this ...

Slick.js integrated with 3D flip is automatically flipping after the initial rotation

I'm encountering an issue with my CSS3 carousel and 3D flipping. Whenever I navigate through the carousel and flip to the next slide, the first slide seems to automatically flip/flop after completing the rotation. You can see a visual demonstration o ...

Is the see-through sticky header going to overlay the main banner?

I recently completed developing a client's website using Squarespace. Now, I am looking to design a header that: 1) Initially has a transparent background upon the user's arrival on the page 2) Transitions to a solid background color and sticks ...