wood burning stoves*
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
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX Form submit problem (JSP/Servlet)" Watch "AJAX Form submit problem (JSP/Servlet)" New topic
Author

AJAX Form submit problem (JSP/Servlet)

Carlos Juan
Greenhorn

Joined: Jul 17, 2006
Posts: 5
Hi,

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.
Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
Greenhorn

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.

Thanks
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
Hi,

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;
request.open("POST", url, false);
request.send(url);

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="http://keelypavan.blogspot.com" target="_blank" rel="nofollow">http://keelypavan.blogspot.com</a>
 
wood burning stoves
 
subject: AJAX Form submit problem (JSP/Servlet)
 
Similar Threads
Notify Users before session expires
JSF 2.0 - replacing JS Alert Box with what?
help pls
onSubmit
Handle multiple click of an image hyperlink