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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two submit buttons 2

Status
Not open for further replies.

florida41

Technical User
May 13, 2004
95
US
I am trying to have two submit buttons where it will submit a different action page depending on which submit button is selected.

Please advise because my attempt not working.

Code:
<script>
function buttonSelect()
{
	if(document.formOne.buttonOne.value == 'buttonOne')
	{
		document.formOne.action = "anotherpage.cfm";
	}
	else if(document.formOne.buttonTwo.value == 'buttonTwo')
	{
		document.formOne.action = "formThree.cfm";
	}
}
</script>
</head>

<body>
<form method="post" name="formOne" onsubmit="return buttonSelect();">
<input type="Text" name="nameOne">
<input type="Text" name="nameTwo">


<input type="Submit" name="buttonOne" value="buttonOne">
<input type="Submit" name="buttonTwo" value="buttonTwo">
</form>


</body>
</html>
 
How about:

<script>
function buttonSelect(url)
{
document.formOne.action = url;
document.formOne.submit();
}
</script>
</head>

<body>
<form method="post" name="formOne">
<input type="Text" name="nameOne">
<input type="Text" name="nameTwo">


<input type="button" name="buttonOne" value="buttonOne" onclick="buttonSelect('anotherpage.cfm');">
<input type="button" name="buttonTwo" value="buttonTwo" onclick="buttonSelect('formThree.cfm');">
</form>
 
The problem with your script is that buttonOne.value is always == to "buttonOne," regardless of which button was pressed.

Try this:

Code:
<script>
[b]var pressed="buttonOne";[/b]
function buttonSelect()
{
    if([b]pressed[/b] == 'buttonOne')
    {
        document.formOne.action = "anotherpage.cfm";
    }
    else
    {
        document.formOne.action = "formThree.cfm";
    }

   [b]return true;[/b]
}
</script>
</head>

<body>
<form method="post" name="formOne" onsubmit="return buttonSelect();">
<input type="Text" name="nameOne">
<input type="Text" name="nameTwo">


<input type="Submit" name="buttonOne" value="buttonOne" [b]onclick="pressed='buttonOne'"[/b]>
<input type="Submit" name="buttonTwo" value="buttonTwo" [b]onclick="pressed='buttonTwo'"[/b]>
</form>


</body>
</html>

I'm pretty sure that the ONSUBMIT function will accept true as the return value by default if you don't specify, but I put it in there anyway.

I didn't test this. Let us know how it works out.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top