wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes Strange - Page Submits!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Strange - Page Submits!!!" Watch "Strange - Page Submits!!!" New topic
Author

Strange - Page Submits!!!

Naani Mui
Greenhorn

Joined: Oct 03, 2004
Posts: 12
I am developing a simple web application. It doesn't have any database connections. Just simple page flows. In all the pages, I am calling a javascript function which validates for the empty or null values of the form fields, and then submits if everything is proper. The problem I am facing is, all the pages which contain a single textbox in the form, gets submitted before the javascript function is processed. Though the javascript function gets called on click of the button, the page gets submitted in the background. But this does not happen in other pages which contain more than one textbox though the function and logic is the same everywhere. Can anyone tell me what could be the problem.

Thanks in advance.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

"Tiger",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

And since this really has nothing to do with JSP, I'm moving it along to the HTML/Javascript forum.
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hi Tiger,

Can you tell, where are you actually submiting the form? Are u doing it by specifying the form action="/servlet/or something" in the form or submitting it explicitly using the Javascript?

Also check this...are the buttons are the type submit or button?

If it is submit, try changing it to button and then try submitting the form.

Cheers,
Swamy
Naani Mui
Greenhorn

Joined: Oct 03, 2004
Posts: 12
I am submitting the form from the javascript function. Only one function in my javascript file contains the submit function call.

Also I am not using any buttons in the form. It contains an image.

WHy I find it strange is, the page works fine on click of the image - it calls the validation functions and if everything is proper, then submits the page. But when I press enter, the page gets submitted without calling any of the javascript functions. I am capturing the key events on the document and if the key pressed is "Enter" I am calling the validation function. The validation function returns true if the form field is NOT NULL. Returns false if it is empty. If true is returned, the page should get submitted. If false is returned, the page should NOT get submitted and it should return focus to the respective form field.
This is not happening on enter press.

The same page works fine when I click on the image for submitting the page.

When I press "Enter" though the validation function is called, the page gets submitted in the background which I can see while the alert is displayed.

All these things happen only if the page contains a single text box. If it has more than one textbox, everything works fine - ALL THE FUNCTIONS AND LOGIC IS THE SAME IN ALL PAGES - ALL THE JAVASCRIPT FUNCTIONS ARE IN A SINGLE JS FILE.





Please help..
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hi Java Rani,
Like Bear Bibeault said please change your display name.

We will try to help you with your quesitons.


Spritle Software Blogs
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Can u give the code?

Cheers,
Swamy
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The default property of a form with a check box is to submit when the enter key is pressed. It sounds like you are doing your validation wrong.

Use the onsubmit property of the form tag to do your validation code.

Eric
Naani Mui
Greenhorn

Joined: Oct 03, 2004
Posts: 12
But, I am using a TEXT box. Not a Check Box.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

What is the 'name' of your submit button? You didn't call it 'submit', did you? If you did, try making sure no elements are called 'submit', rename them 'btnSubmit' or something.

Dave
Naani Mui
Greenhorn

Joined: Oct 03, 2004
Posts: 12
html:form name="DummyForm" type="org.apache.struts.action.DynaActionForm"action="/feedback"


INPUT TYPE=TEXT NAME="tb1" VALUE="" MAXLENGTH=7 SIZE=7

A href="javascript:fnFeedBack('x','y','z')"
img src="images/update.gif" ALIGN=TOP width="97" height="26" border="0"
/A


script Language="JavaScript"
var thisForm = document.DummyForm;
isFullyLoaded = true;

var tb1 = thisForm.tb1;

function checkForm()
{
var fieldHandle = thisForm.tb1;
var fieldValue = fieldHandle.value;
if (!isInteger(fieldValue))
{
//return sendErrorMessage(fieldHandle, getisIntegerErrorMessage());
}
if (isEmpty(tbQuestion_1.value) || (tbQuestion_1.value < 0))
{
alert("Please enter a whole number of 0 or greater.");
getFocus(tbQuestion_1);
return false;
}
return true;
}
function isEmpty(s)
{
for (i = 0; i < s.length; i++)
{
if (s.charAt(i) != " ")
return false;
}
return true;
}
function isInteger(s) {

if (isNaN(s) || isNaN(parseInt(s)) || (parseInt(s) != parseFloat(s)) || (s.indexOf(".") >= 0) )
{
if (!isEmpty(trim(s)))
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function fnFeedBack(a,b,c)
{
if(checkForm())
{
fnSubmit(a,b,c);
}
}
function fnSubmit(aa,bb,cc)
{
thisForm.submit();
}
var isNav, isIE;
document.keyup = checkChar;
function checkChar(evt)
{
var Key;
if (isNav)
{
Key = evt.which;
}
else
{
Key = window.event.keyCode;
}

if(Key == 13)
{
if (isIE)
{
fnEnterPressed();
}
else
{
isNavEnterHit = true;
}
}
}
function fnEnterPressed()
{
fnFeedBack('x','y','z');
}
/script
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
i meant text box, not sure why I typed check...
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
Not sure, if this is the issue, but:
Since the form contains only 1 element, which is text, and you dont have any handler for onsubmit in a form tag, form will get submitted, if user hits "Enter".
You should handle keydown opose to keyup.
[ October 05, 2004: Message edited by: Yuriy Fuksenko ]
Naani Mui
Greenhorn

Joined: Oct 03, 2004
Posts: 12
THANKS A LOT. I WAS ABLE TO FIX THE PROBLEM BY ADDING SOME JAVASCRIPT IN THE FORM ONSUBMIT EVENT.

THANKS ONCE AGAIN.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange - Page Submits!!!