Forum Moderators: open

Message Too Old, No Replies

Submitting Forms

Passing Variables to Javascript

         

chrisj123

10:40 am on Feb 5, 2004 (gmt 0)

10+ Year Member



OK, I know very little about javascript and I don't really like it. I've got a little bit here to submit a form using an image rather than the standard HTML interface. This works so long as there is just one form on the page (<form name=form1 ..>)

function resetForm() {
document.form1.reset()
}

function submitForm() {
document.form1.submit();
}

And, for the HTML

<a href="javascript:submitForm()"> IMAGE HERE </a>

I now have several forms on a page which are each built using hidden elements takenfrom a database. PHP builds each form and each form needs a seperate name and submission button.

How can I use multiple forms? I'm guessing I just need to pass a variable to the javascript but I don't know how.

Could anyone help and stop me feeling like a proper wally?

Thanks,

Chris J

dnimrodx

11:16 am on Feb 5, 2004 (gmt 0)

10+ Year Member



How can I use multiple forms? I'm guessing I just need to pass a variable to the javascript but I don't know how.

Chris, you do need to pass the form ID reference to the javascript function you wish to call.

Take a look at this sample code:


function getElement(elem)
{
// test which method is supported by the browser
if(document.getElementById)
return getElementById(elem);
else if(document.all)
return document.all[elem];
return null;
}

function resetForm(formID)
{
// does the form exist?
if((elem = getElement(formID))!= null)
{
// it does exist,
// do whatever you want with the element here (in this case, the form)
elem.reset();
}
}

function submitForm(formID)
{
// does the form exist?
if((elem = getElement(formID))!= null)
{
// it does exist,
// do whatever you want with the element here (in this case, the form)
elem.submit();
}
}

in the HTML, when calling the functions, always pass the ID of the form enclosed in '_____' to the javascript function you wish to call,

<a href="submitForm(
'MYFORMID'
)"> IMAGE HERE </a>

I hope this helps, :)

Regards

d#Nimrod

BTW: I tried to indent the code above, but somehow the code that powers the forum does not allow it.

chrisj123

12:06 pm on Feb 5, 2004 (gmt 0)

10+ Year Member



Thank you Nimrod,

I'm sorry but I can't get that to work for some reason.

I've just tried using

<a href="#anchor" onClick="document.form1.reset()> IMAGE HERE </a>

and it looks like it will work OK because I can change the 'form1' part with PHP as I draw up the forms.

Is there any reason not to do this? It seems simple enough.

Thanks again

Chris J

dnimrodx

12:47 pm on Feb 5, 2004 (gmt 0)

10+ Year Member



Sorry Chris... there are a few mistakes in the page which I have corrected.

The following (updated) code works just fine with my IE:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<SCRIPT type="text/javascript" language="JavaScript">
function getElement(elem)
{
// test which method is supported by the browser
if(document.getElementById)
return document.getElementById(elem);
else if(document.all)
return document.all[elem];
return null;
}

function resetForm(formID) 
{
// does the form exist?
if((elem = getElement(formID))!= null)
{
// it does exist,
// do whatever you want with the element here (in this case, the form)
elem.reset();
}
}

function submitForm(formID) 
{
// does the form exist?
if((elem = getElement(formID))!= null)
{
// it does exist,
// do whatever you want with the element here (in this case, the form)
elem.submit();
}
}
</SCRIPT>
<body>
<form name="MyFormID" method="post" action="">
<p>
<input type="text" name="TextFieldID">
</p>
<p>&nbsp; </p>
<p>
<a href="javascript:submitForm('MyFormID');"> IMAGE HERE </a> </p>
</form>
</body>
</html>

But let me ask you: do you really need javascript functions to process the form's submit/reset?

Or is your problem just related with not being able to handle multiple forms in the same page? I mean, if you only want to submit/reset forms without any other additional processing, you don't really need javascript.

d#Nimrod

chrisj123

1:51 pm on Feb 5, 2004 (gmt 0)

10+ Year Member



If I'm honest then no, I don't NEED javascript to handle the form. I want to use my own images as buttons, using DreamWeaver's rollover script, purely to add a little design to what is otherwise a very plain site.

I've built the rest of the site with one form per page and the image buttons had worked fine until I hit this snag with multiple forms.

Your script works well in IE, brilliant. I can't get it to work in Firebird which is my default browser. As this is an academic project I need it to work cross-browser.

Thanks again,

Chris J

dnimrodx

2:34 pm on Feb 5, 2004 (gmt 0)

10+ Year Member



Your script works well in IE, brilliant. I can't get it to work in Firebird which is my default browser. As this is an academic project I need it to work cross-browser.

Sorry about that chris... but give me a sec and i'll try to see what I can come up to, okay?

dnimrodx

2:54 pm on Feb 5, 2004 (gmt 0)

10+ Year Member



Right Chris I have found a way which will work just fine:

in all your

<a href="...
for form submission or reset, do the following:

If you want to submit:

<a href="window.document.MyFormID.submit()"> IMAGE HERE </a>

to reset:

<a href="window.document.MyFormID.reset()"> IMAGE HERE </a>

'MyFormId' must be changed by the ID of the form you are working on.

i.e.:

<form name="ThisIsFormI" method="post" action=""> 
<p>
<input type="text" name="TextFieldID">
</p>
<p>&nbsp; </p>
<p>
<a href="window.document.ThisIsFormI.submit()"> IMAGE HERE </a>
<a href="window.document.ThisIsFormI.reset()"> IMAGE HERE </a>
</form>

<form name="MySecondForm" method="post" action=""> 
<p>
<input type="text" name="TextField">
</p>
<p>&nbsp; </p>
<a href="window.document.MySecondForm.submit()"> IMAGE HERE </a>
<a href="window.document.MySecondForm.reset()"> IMAGE HERE </a>
</form>

It should work well in Mozilla Firebird.
Try this, and plase let me know how it turned out to be (I am curious now) :)

d#Nimrod

chrisj123

7:39 pm on Feb 5, 2004 (gmt 0)

10+ Year Member



Ah ha!

<a href="window.document.MyFormID.submit()">

Does not work, but

<a href="javascript:window.document.MyFormID.submit()">

does.

Same with reset(), 'the javascript:' bit must be at the front other wise the browser tries to load
hxxp://www.myhost.com/window.document.MyFormID.submit()/
which, obviously, doesn't work.

Also, to make things shorter, you can leave out 'window.' and start from 'document.'

Thanks again Nimrod, it's been a long day and I'd have been screwed without your help.

Now, where's that Jack Daniels gone.....

Chris J

dnimrodx

12:26 pm on Feb 6, 2004 (gmt 0)

10+ Year Member



Thanks again Nimrod, it's been a long day and I'd have been screwed without your help.

Don't mention it -- I am glad I was helpful.

Now, where's that Jack Daniels gone.....

Well, when you do find that Jack Daniels bottle, I wouldn't really mind having a drink or two... that would settle our scores. [lol]

:)

David