The onsubmit function is not functioning correctly in combination with Ajax

My current implementation involves utilizing Ajax and Php for user login validation. Unfortunately, the form onsubmit function is not properly functioning.

<form action="loggedin.php" onsubmit="return test()" method="post">

Below is the corresponding Javascript code:

function test(){
    var a = document.getElementById("email").value;
    var b = document.getElementById("pwd").value;                
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
    if(this.readyState == 4 && this.status == 200){
        var resp = this.responseText;
        if(resp != "Correct"){
            document.getElementById("email").value = "";
            document.getElementById("pwd").value = "";
            document.getElementById("passdiv").style.display = "block";
            return false;
        }
        else{
            return true;
        }
    }
    else{
        return false;
    }
    };
    xmlhttp.open("POST","check.php",true);
    xmlhttp.send("email=" + a + "&passwd=" + b);
}

Can anyone shed light on why the onsubmit function is not working as intended?

Answer №1

onclick="validate()"

Avoid using return statement.

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

Alert message in jQuery validation for the chosen option value

I am attempting to validate a combo box with the code provided below. Currently, I receive multiple alert messages even if one condition is true. My goal is to only display one alert message when a condition is met and highlight the other values in red. Th ...

Tips for managing a date picker with JavaScript using the Selenium WebDriver

I have been attempting to scrape a travel website using Selenium Webdriver and Python. While I have successfully set the destination (destino) and place of origin (origem), I am encountering difficulties when trying to select a date. I understand that Ja ...

Guide to retrieving IDs of images on a canvas during drag and drop functionality

I've developed a finite state machine drawing tool that includes drag-and-drop functionality for different images, such as states and arrows. Each arrow creates a div tag for the transition, with unique ids assigned to every state, arrow, and transiti ...

Using Highcharts to display series data retrieved from a JSON object

Recently diving into JSON and MVC, I encountered an issue while working on graphs with Highcharts. In my controller, I have a method that returns a JSON object. public JsonResult _GetChart_TrendPublicationTypeDetailed_Data(int yearToDisplay) { ...

Utilize React to generate HTML content and send it as the email body through Node.js

I am currently working on a react application that consists of two main pages - AddStatus and ViewStatus. Both of these are implemented as react components. My current task involves sending out an email daily at a specific time, containing the details dis ...

An error popped up: "argument list is missing) after"

Encountered an issue: Error message: missing ) after the argument list $('.next-btn').append("<a class="actionsubmit" ng-click="onSubmit('hello.html')">Check</a>"); ...

Determining the largest range possible in a sorted array of integers

I need help with a JavaScript implementation for the following challenge. Imagine we have a sorted array: [1,2,5,9,10,12,20,21,22,23,24,26,27] I want to find the length of the longest consecutive range that increments by 1 without duplicates. In the ...

Is it possible to invoke a computed property within the props of a child component in Vue.js?

In the context of my child and parent components, I have a boolean prop in the child component with a default value of false. When calling the child component within the parent component and setting it based on a computed function that returns a boolean va ...

The one-click button is functional on larger screens, while on mobile devices, only a double click will register

I am facing an issue in angular where a button works perfectly fine with one click on larger screens, such as Macbook. However, on iPhone, it requires a double click to function properly (it works fine on Android too). The alert triggers with a single cl ...

Exploring the world of Ajax with jQuery

Could someone help me figure out how to integrate Ajax into my PHP code so that the content can load dynamically? Currently, it looks something like this: First, a user selects a category: <li><a href='?genre=sport'>Sport</a>& ...

React: Unexpected error occurs with invalid element type

I encountered the following issue while attempting to utilize a component Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forg ...

Angular JS and TypeScript - Issue: ng:areq Invalid Argument "Argument 'XXXXXX' is not a function, received undefined"

Encountering a specific error mentioned in the title. I organized models and controllers into distinct files saved under models and controllers folders respectively. Upon trying to establish a connection between them, I received an error stating "ng:areq ...

Ways to serve JSON response following a 400 error code

After a user submits incorrect form details, such as an invalid username or blank email address, I make an Ajax request to my REST API. The response data I receive is structured as follows: HTTP 400 Bad Request Allow: POST, OPTIONS Content-Type: applicati ...

Is there a pub/sub framework specifically designed for managing events in Angular?

Having a background in WPF with Prism, I am familiar with the IEventAggregator interface. It allows you to define events that can be subscribed to from controllers and then triggered by another controller. This method enables communication between controll ...

What is the best way to implement an onReady promise in a JavaScript application?

Exploring some Advanced topics in JS I am currently diving into and there is an interesting concept that's piqued my curiosity. I've been developing a VueJS application and now I'm looking to expose certain data and even methods from Vue ou ...

Joomla presents a bare XML file lacking any style information. Below displays the document tree

After setting up a component for Joomla called MijoPolls, everything was running smoothly until I turned on AJAX voting. Suddenly, I encountered an error when trying to vote. However, the strange thing is that when I voted while browsing the Kunena forum, ...

If the option is 'Gel', use an if statement to console log

I'm trying to console.log the option from the data() function that is equal to 'Gel'. I attempted to use an if statement, but it doesn't seem to be working. Anyone have any ideas on how to make this work? data(){ return { ...

Challenges Arising from Dynamic Content in jQuery Booklet by BuiltByWill

I'm currently facing a number of issues. Firstly, this code only seems to be functioning in Firefox. Chrome doesn't display any errors but fails to trigger the alert in the 'after' section. Secondly, my main concern is that while the co ...

Encountering an issue stating "The element is not visible at the moment, therefore cannot be interacted with" while trying to access a text box within a window opened via an ajax call

Currently, I am working on a Selenium script using Java to automate a specific process. However, I have encountered an issue where clicking on a dropdown causes a hidden popup to appear through an ajax call. Unfortunately, when attempting to interact with ...

Creating a dynamic shift in background color

Is it possible to use jQuery to slowly change the color of diagonal lines in a background styled with CSS, while also adding a fading effect? I have created a fiddle with the necessary CSS to display a static background. You can view it here: http://jsfid ...