Forum Moderators: open

Message Too Old, No Replies

Not able to get a value

         

vjy143

11:41 am on Apr 11, 2005 (gmt 0)

10+ Year Member



Hi,
I am using jsp, java and javascript in my development. Basically I need to sort elements by columnwise, so I have to set the initial parameter as null and then based on the selection I am using javascript to assign the value. eg

Javascript Function
-------------------
function getSorted(strOption, param, flag) {

alert("strOption: "+strOption);
alert("param: "+param);
alert("flag: "+flag);

var a = document.form.sortName.value;
if(a!=null)
{

}
else
{
alert("value is null");
}

document.form.sortName.value ="hai";
alert("document.form.sortName.value: "+document.form.sortName.value);

document.form.sortName.value = strOption;
alert("document.form.sortName.value: "+document.form.sortName.value);

if(param==1) {
document.form.sortName.value = strOption;
alert(document.form.sortName.value);
document.form.flagSet.value = flag;
document.form.method="post";
document.form.action="abc.jsp";
document.form.submit();
}
}
----------------------

I am calling this from jsp like

<img src="images/ship_name.gif" width="113" height="26" name="shipName" onClick="getSorted('shipName',1,2)" >

----------------------

The same thing works if the page where I am using this code is stand alone, but doesnt work if I include another page. I am not getting the "sortName" value at all.

What am I missing? Also I read in some forums that if I have 2 forms in the jsp page, it mght give this error and I do have one for the included page and one for the including page and both have the same form name.

If anyone wants, I can post the whole code.

Thanks

rocknbil

5:15 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not getting the "sortName" value at all.

See if this gets you pointed in the right direction.

First,

var a = document.form.sortName.value;

Is your form named "form"?
<form action="whatever" name="form">

Because that's what it appears you're looking for. If that's the case, I would NOT name the form "form" name it something else, like my_form, whatever.

Second,

if(a!=null) { ... }

I'm a bit out of turn here because I don't recall the exact conditions for this - but I do know that null is not always what you think it should be. The text field may not be NULL. After you set it to the form value, it's a blank field. Try

if (a!= '') { ... }

or just

if (a) { ... }

Lastly here's a few things to play around with. The comment about two forms - this is only true if you don't reference the correct one when you call the function. A way of working with this is to not reference some objects expicitly by name, just pass the object:

<img src="images/ship_name.gif" width="113" height="26" name="shipName" onClick="getSorted(this,this.form,1,2)" >

function getSorted(strOption, form, param, flag) {
var a = strOption.value;
if(a!= '') {
alert('here is the value ' + a);
}
else
{
alert("value is not exactly null, but blank.");
}

Not that instead of passing the STRING, it's just the object representing that field by the "this" keyword. So now object.name, object.value, etc, are all available to your routine.

After recoding that - I saw that there was no reason to pass the FORM object - but you may want to do that anyway, as you may need it later

form.submit();

In this context, form is the form OBJECT. Not a name.

vjy143

5:19 am on Apr 13, 2005 (gmt 0)

10+ Year Member



Thanks for ur help dude.
I finally got it working.
The problem was like a form inside a form i.e when including the page(included page is also a form).This I got to know by view source and it looked like this

<form name = a>
<form name = b>

</form>
</form>

and I read a tutorial that it should not be done this way. So I changed it to

<form name = a>
</form>

<form name = b>
</form>

This worked great.

Thanks for the help once again