| 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
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
|
Your valCheck function doesn't return anything. It would need to return false to keep the form from submitting.
|
 |
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>
|
 |
 |
|
|
subject: AJAX Form submit problem (JSP/Servlet)
|
|
|