PDA

View Full Version : Quick question: form submit value with javascript


tatlar
02-26-2004, 04:26 PM
I want to submit a form with a hyperlink instead of a submit button, thus:

<a href="javascript:document.myform.submit()">Graphics</a>

BUT, can I also add a name-value pair with this like I can with a submit button?

<input type="submit" name="graphics" value="Graphics">

So what I want is something like:

<a href="javascript:document.myform.graphics.graphics.submit()">Graphics</a>

Would that even work? All help appreciated! Thx!

tatlar
02-26-2004, 05:05 PM
This should be possible because the following elements exist for the submit object in the DOM:

submitName.propertyName
submitName.methodName(parameters)
formName.elements[index].propertyName
formName.elements[index].methodName(parameters)

I am stuck as to how to make this work... :(

Jon Hanlon
02-26-2004, 05:05 PM
I take it you're trying to send extra information with your form.
Form elements are special html elements called controls. Controls differ because they send down name/value pairs when the form is submitted. Most html elements aren't controls, nor can they be turned into controls.

What you need to do is add an extra (hidden) field to your form, then set it from the link:

<a href="document.forms.myForm.secret.value='shark';document.forms.myForm.submit()">Shark Submit</a>
<br>
<a href="document.forms.myForm.secret.value='whale';document.forms.myForm.submit()">Whale Submit</a>
...
<form name="myForm" ...>
<input type="hidden" name="secret" value="">
...
</form>

tatlar
02-26-2004, 05:07 PM
Thanks Jon. I will try this out.
Champion! :D

tatlar
02-26-2004, 05:16 PM
great - that worked. thanks mate.