...

View Full Version : Submitting a form with a html link



Gil
08-06-2002, 04:41 PM
Hi,

Can someone explain the next thing to me. I can submit a form with the following button and pas a value for the field "testfield":

<input type="submit" Onclick="this.form.testfield.value='myvalue'">

If I want to do the same with a textual html link it will only work if the form has a name, so I gave the form the name test:

<a href="#" Onclick="test.testfield.value='mysecondvalue';test.submit()">Submit</a>

I must be doing something wrong if this isn't a 'feature' in HTML

I I use:

<a href="#" Onclick="this.form.testfield.value='myvalue';this.form.submit()">Submit</a>

it will not be submitted.

Can someone explain this to me so I can make it work?

....thanks a BIG lot,

Gil

beetle
08-06-2002, 06:09 PM
In order for this.form to function, the object must be part of the form's elements collection, which any anchor object is not (only INPUT, TEXTAREA, SELECT). Instead, you can reference the form by it's index in the document's form collection:

document.forms[0].submit();

This, of course, assumes that the form is either the only form on the page, or the first form to appear in the HTML.

adios
08-06-2002, 08:42 PM
this, inside any HTML event handler, refers to the element (object) the handler is a property of. this.form works for form elements, which have a specific property named .form, exposed for just this purpose - to let you step 'up one level' to the Form object. this within a Link object's event handler refers to - what else - the Link object; no .form property there. Your posted link should work in Internet Explorer, which exposes ids/names as global variables; for cross-browser purposes:

<a href="#" onclick="document.test.testfield.value='mysecondvalue';document.test.submit()">Submit</a>

window is implicit; document ain't.

Gil
08-06-2002, 09:14 PM
Thanks a loth to the both of you. It's clear to me now.

Gil



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum