PDA

View Full Version : Creating a form and elements


mgersting
12-31-2002, 12:17 AM
Hey all, thanks in advance for your help.

What I'd like to do is to create a script that creates and submits a form that does not already exist on the page. This way, this function can be attached to a normal link and would allow me to send variables via POST using a standard anchor.

I've been playing with this for awhile and have not had much luck. Here's a sample prototype:

PostVal ( formName, valToSend, action);

The script would create the form object, a related text field, set the action and then submit the form.


Any tips would be greatly appreciated!

beetle
12-31-2002, 10:01 AM
Well, here's a simple layout that provides a foundation for lots of building blocks, depending on how complicated you need to get.<html>
<head>
<title>Auto form</title>

<script type="text/javascript">

function ObjectForm( a, m, id, o )
{
var f = document.createElement( "form" );
f.setAttribute( "action", a );
f.setAttribute( "method", m );
f.setAttribute( "id", id );
this.f = o.appendChild( f );
}

ObjectForm.prototype.addInput = function( t, n, v )
{
var i = document.createElement( "input" );
i.setAttribute( "type", t );
i.setAttribute( "name", n );
if ( typeof v != 'undefined' )
i.setAttribute( "value", v );
this.f.appendChild( i );
}

ObjectForm.prototype.sbmt= function()
{
this.f.submit();
}


function doForm()
{
var frm = new ObjectForm( "test2.htm", "post", "myForm", document.body );
frm.addInput( "text", "Name", "beetle" );
frm.sbmt();
}

</script>

</head>

<body>

<a href="javascript:doForm()">Click here</a>

</body>
</html>

mgersting
12-31-2002, 09:02 PM
Thanks, beetle. I will take that and see what I can come up with. I'll let you know.

mgersting
01-02-2003, 11:57 PM
Works great! Took me a few minutes to figure out how everything worked, but I got the hang of it.

Thanks a ton!

whammy
01-03-2003, 01:18 AM
Is this IE only?

jkd
01-03-2003, 01:29 AM
Originally posted by whammy
Is this IE only?

Looks to me to be DOM1 Core compliant scripting. IE, Mozilla, Opera 7, Konqueror (?), etc.

whammy
01-03-2003, 01:29 AM
Thanks! BTW, your website is good ;)