Form with onSubmit on a JSP

3k views Asked by At

I've got this form on a JSP:

<form name="<%=formName%>" method="post" action="<%=actionURL%>" target="_parent" onsubmit="submitForm('<%=String.valueOf(isAbstract)%>','<%=formName%>'); return false">

Then I have the following Javascript in an include file:

function submitForm( isAbstract , formName ) {
    var o = "document.forms['" + formName + "']";
    var form = eval(o);
    if (isAbstract)
    {
            alert("Error");
            return;
    }
    if (!form.AutoName.checked)
    {
        if (form.name.value == "")
        {
            alert("Some text");
            return;
        }
    }
}

Well, I reduced the functions to make it simpler, but the thing is that whatever I do, I keep getting undefined values for both arguments. This is driving me crazy, because I've got similar code on other tags that work just fine (except that it's on a combo box tag with the onChange action).

Any thoughts, please?

2

There are 2 answers

1
user243542 On

I see that you are submitting the form to parent frame by using target attribute. Are you submitting the values from child iframe to parent window? If yes, does this form exist in parent frame?

Alterantively, you can simply pass "this" and access that as a form object.

For example,

<form onsubmit="submitForm(this)">

function submitForm(formObj){
  var fieldTxt1Value = formObj.txt1.value;

}

2
Gustavo Costa De Oliveira On

try to place a return false; At end of your func.

Looking your code, you're testing if the first parameter is true. But it is a string all the time, because you write quotes in call. It's what you want?