Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form submission problem

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
I'm not even sure if this is possible with javascript. I have multiple forms on the page that will be generating from a dynamic list of links. I don't want to pass variables in the URL so I'm posting form data to the receiving page. and using submit buttons descised as text links to send information. Well anyway here is the code, it doesn't work and I can't figure out what is wrong... please help.

<head><script>function submitMe()
{
alert('In submitMe()');
document.submitForm[].submit();
return;
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form name="submitForm[0]" action="rec.php" method="post">
<input type="hidden" value="page0" name="id">
<input type="hidden" value="page0" name="action">
<a href="javascript:document.submitForm.submit()">submit direct</a><br>
<a href="javascript:submitMe()">submit via a function</a><br>
<a href="javascript:document.submitForm.submit()">test</a><br>
</form>
<form name="submitForm[1]" action="rec.php" method="post">
<input type="hidden" value="page1" name="id">
<input type="hidden" value="page1" name="action">
<a href="javascript:document.submitForm.submit()">submit direct</a><br>
<a href="javascript:submitMe()">submit via a function</a><br>
<a href="javascript:document.submitForm.submit()">test</a><br>
</form>
<form name="submitForm[2]" action="rec.php" method="post">
<input type="hidden" value="page2" name="id">
<input type="hidden" value="page2" name="action">
<a href="javascript:document.submitForm.submit()">submit direct</a><br>
<a href="javascript:submitMe()">submit via a function</a><br>
<a href="javascript:document.submitForm.submit()">test</a><br>
</form>

</body>
 
You shouldn't use [] in the naming scheme for your forms. I know that PHP sometimes forces users to use brackets when it deals with arrays, but if you're not using PHP then you should get rid of the brackets.

All that aside, this should work for you:
Code:
<a href="#" onclick="this.form.submit();return false">submit direct</a><br>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
uncle_rico_thumb.jpg
 
I'm not sure what I'm doing wrong. This still isn't working. I took out the [] from the entire form. I'm guessing the the "return false" is what i'm using so that only the form that is clicked on is submitted... correct???
Then I tried several combinations of what you wrote and none of them worked. Here is what I have now please take a look at it and tell where my mistake is.

wasn't I supposed to change in the link "this.form.submit" to "document.submitForm.submit" ???


<head><script>function submitMe()
{
alert('In submitMe()');
document.submitForm.submit();
return;
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form name="submitForm" action="rec.php" method="post">
<input type="hidden" value="page0" name="id">
<input type="hidden" value="page0" name="action">
<a href="#" onclick="document.submitForm.submit();return false">submit direct</a><br>

<a href="javascript:submitMe()">submit via a function</a><br>
<a href="javascript:document.submitForm.submit()">test</a><br>
</form>
<form name="submitForm" action="rec.php" method="post">
<input type="hidden" value="page1" name="id">
<input type="hidden" value="page1" name="action">
<a href="#" onclick="this.form.submit();return false">submit direct</a><br>

<a href="javascript:submitMe()">submit via a function</a><br>
<a href="#" onclick="this.form.submit();return false">test2</a><br>

</form>
<form name="submitForm" action="rec.php" method="post">
<input type="hidden" value="page2" name="id">
<input type="hidden" value="page2" name="action">
<a href="#" onclick="this.form.submit();return false">submit direct</a><br>

<a href="javascript:submitMe()">submit via a function</a><br>
<a href="#" onclick="this.form.submit();return false">test3</a><br>

</form>

</body>
</html>
 
Why are all your forms named the same? You're looking for ways to mess things up doing this kind of thing. kaht's suggestion is a VERY sensible method with what you have.

Lee
 
I'm trying to use Kaht's idea, i'm not understanding how to make it work. are you saying that I don't need the top portion of my script in the <head> tag and to just use the one line from Kaht??
so my links should look like this

<a href="#" onclick="this.form1.submit();return false">submit direct1</a><br>
<a href="#" onclick="this.form2.submit();return false">submit direct2</a><br>
<a href="#" onclick="this.form2.submit();return false">submit direct3</a><br>

and remove this from the <head> completely..
<head><script>function submitMe()
{
alert('In submitMe()');
document.submitForm.submit();
return;
}
</script>


I'm sorry, i'm just still learning how to write javascripts.
 
That's not what kaht provided. With his example, you can use your original naming convention for the forms.

If you want to use Javascript's reserved characters in form names, you need to refer to the form in the document's associative array:

Code:
document.forms['formnamehere'];

But for that to work with your original code, you'd have to pass the PHP array index to the function.

You could use this variation of your original code:
Code:
<script type="text/javascript">
function submitMe(oneform)
{
alert('In submitMe()');
oneform.submit();
return;
}
</script>

and

Code:
<form name="submitForm[0]" action="rec.php" method="post">
<input type="hidden" value="page0" name="id"> 
<input type="hidden" value="page0" name="action">
<a href="javascript:this.form.submit()">submit direct</a><br>
<a href="javascript:submitMe(this.form)">submit via a function</a><br>
<a href="javascript:this.form.submit()">test</a><br>
</form>

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top