Here’s the snapshot of the code:

If the function testFields at Line:3 returns false, the control rightly goes to Line:21 and returns false. If testFields returns true, the control goes to Line:4 and then instead of going on to handle the response returned in function at Line:5, the control goes on to Line:21, where the retVal is still false. That is, condition to Line:6 is checked after the function has returned a value to the function:
<form method="POST" onsubmit="return validateInput()" />
Where as, logically, it should first test the condition at Line:7 then decide whether retVal should be true or let it be default false and then value should be returned via Line:21.
What is logically wrong with my code?
There are two ways.
You can make synchronous ajax request.
But if you don’t want the script been stopped. You can prevent a default action in form, and send the form from callback such as:
and in html form tag without onsubmit declaration :