File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes AJAX Form submit problem (JSP/Servlet) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX Form submit problem (JSP/Servlet)" Watch "AJAX Form submit problem (JSP/Servlet)" New topic

AJAX Form submit problem (JSP/Servlet)

Carlos Juan

Joined: Jul 17, 2006
Posts: 5

I have a form and onsubmit calling a javascript function to make sure some value does exists in database. If the value exists I
am submitting a form but if the value does not exists I don't want to submit a form rather show user a message. I am trying to use
AJAX, it does go to the databse and checks for the required value but whatever the value is it still keeps submiting the form. Can someone please
help me what am I doing wrong here. Here is my code:

In my JSP I have the following piece:

Here is javascript:

In onResponse function above no matter if it returns true or false the FORM keeps on submitting. Any help is appreciated.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Your valCheck function doesn't return anything. It would need to return false to keep the form from submitting.

GenRocket - Experts at Building Test Data
Carlos Juan

Joined: Jul 17, 2006
Posts: 5
Thanks, It's working just did some testing and so far all ok, It works in IE but some how FireFox it does not work. I don't know if it's because of the following or am I missing something else:

Here is the javascript change I made.

Is there anything else I have to do to make it work in FireFox/Netscape. Any help is appreciated.

Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62

Seems like you have basic misunderstanding about Ajax. You are making synchronous calls, not "A"synchronous calls.

Second thing I want to say is, there is no need for callbacks if you are using synchronous calls. You unnecessarily complicated the code by having those callbacks for synchronous requests.

function valCheck(url)
//url is servlet that inturn query a database a returns a value
request = createRequestObject();
request.onreadystatechange = onResponse;"POST", url, false);

if( request.status == 200 ) {
var str = request.responseText;
// do whatever you are doing in callback handler.
else {
//write error handling code here.

If you really mean to use asynchronous calls, then change your submit button to normal button and onclick process the asynchronus call and submit form once you get confirmation from server.

Pavan Keely<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: AJAX Form submit problem (JSP/Servlet)
It's not a secret anymore!