JSP form getting submitted twice

Hi,

The html form is getting submitted twice...that is it invokes the servlet twice and I see the servlet getting executed twice...there is NO GET method calling POST or viceversa...any help is appreciated...I tried a lot of possible solutions but in vain...

thanks,

Sri

JSP Code:-->>>>>>>>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

<META HTTP-EQUIV="Expires" CONTENT="-1">

<script language="javascript">

function evalCall(object)

{

//alert("length is-->" + object.value.length);

var control= document.getElementById('testForm');

if(object.value.length==2)

{

control.action="SearchServlet";

control.method="post";

control.submit();

//return false;

}

else

{

return;

}

}

</script>

</head>

<BODY>

<form id="testForm">

<table>

<tr>

<td>Enter the city name

<input type="text" id="searchValue" name="searchValue" onkeyup="evalCall(this)">

</td>

</tr>

</table>

</form>

</BODY>

</HTML>

[1376 byte] By [DevStationa] at [2007-11-27 6:03:54]
# 1

I really don't see any reason for the form to get submitted twice....

for debugging at the form level....

you can put an javascript alert() to check what is happening..and how many times you r getting the alert message.......like the following .........

function evalCall(object)

{

//alert("length is-->" + object.value.length);

var control= document.getElementById('testForm');

if(object.value.length==2)

{

control.action="SearchServlet";

control.method="post";

alert("Going to Submit");

control.submit();

//return false;

}

else

{

return;

}

}

By that time if you can tell us...why u need a keyUp event to be fired from ur textbox....probably we can make out a different approach to do the same utility....

thanks

anurag_dasha at 2007-7-12 16:47:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi Anurag,

Thanks for the suggestion. This behavior happens randomly- and after novice attempts of GUI - the thing I found out isif I type in "X", keyup, type in "E"- it gets submitted once...but if I try to type as in a regular editor...say keyin "X" and immediately keyin "E" the javascript is submitting called twice with the input "XE"...I am using IE 7 as browser...

I tried the alert approach suggested by you I see only one alert popping up but double submission of form- coz the keystroke nulls the alert(maybe)

Thanks

DevStationa at 2007-7-12 16:47:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I am using the onkeyup because am trying to implement an AJAX behavior. used the same event earlier, worked perfectly!
DevStationa at 2007-7-12 16:47:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

So we have a probable concurrency thing happening.

Javascript is often compiled on the fly.

If you type the letters quickly, by the time it runs the code for pressing the first key, you may have actually entered the second key, and object.value will be two characters long.

The event fires twice because you pressed 2 keys.

It submits twice because both times when it ran, it found a value two characters long.

possible solution: Make it a bit smarter so that it will only submit once:

var formSubmitted = false;

function evalCall(object)

{

//alert("length is-->" + object.value.length);

var control= document.getElementById('testForm');

if(object.value.length==2 && !formSubmitted)

{

formSubmitted = true;

control.action="SearchServlet";

control.method="post";

control.submit();

//return false;

}

evnafetsa at 2007-7-12 16:47:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
works perfect!!!thanks a ton!!!
DevStationa at 2007-7-12 16:47:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...