Page 1 of 1

JS Submit

Posted: Wed Jun 20, 2007 1:08 pm
by tecktalkcm0391
I have to submit buttons...

Code: Select all

<input type="submit" name="action" value="Forward" />
<input type="submit" name="action" value="Back" />
I want to have a javascript that "clicks" Foward, or Back and submits the correct action. Any ideas how to do it/help?

Posted: Wed Jun 20, 2007 3:04 pm
by califdon
If I understand what you are trying to do, the answer is that you can't do it with a Submit button. A Submit button is part of a form and the Submit button submits the form's data to whatever script is indicated in that form's Action= property. So any particular form can only have one Action. The Submit button merely triggers that action.

One way you might accomplish what I think you're trying to do is to use <a> hyperlinks surrounding two <input type='button'>s and sending the form data as GET parameters. Ugly, but might do what you need.

Posted: Wed Jun 20, 2007 4:22 pm
by Gente

Code: Select all

	<script type="text/javascript">
		function goto(_action)
		{
			document.forms.mainform.action = _action;
			document.forms.mainform.submit();
		}
	</script>

Code: Select all

<form action="#" id="mainform" method="post">
   ...
  <input type="button" onclick="goto('frw.php');" name="forward" value="Forward" />
  <input type="button" onclick="goto('back.php');" name="back" value="Back" />
  ...
</form>

Re: JS Submit

Posted: Wed Jun 20, 2007 4:25 pm
by Gente
One more thing...
tecktalkcm0391 wrote:

Code: Select all

<input type="submit" name="action" value="Forward" />
<input type="submit" name="action" value="Back" />
Please pay attention on the names of your buttons. Every name should be unique.

Posted: Wed Jun 20, 2007 4:36 pm
by Weirdan

Code: Select all

  <input type="button" onclick="goto('frw.php');" name="forward" value="Forward" /> 
And how would that behave if I simply pressed 'Enter' while one the form's fields had been having the focus? To me it seems that catching form's 'onsubmit' event is much better approach.

Posted: Wed Jun 20, 2007 4:49 pm
by Gente
Weirdan wrote: And how would that behave if I simply pressed 'Enter' while one the form's fields had been having the focus? To me it seems that catching form's 'onsubmit' event is much better approach.
Ok. We can choose default form action which will call when we press enter.

Code: Select all

<form action="#" id="mainform" method="post">
   ...
  <input type="button" onclick="goto('frw.php');" name="forward" value="Forward" />
  <input type="button" onclick="goto('back.php');" name="back" value="Back" />
  ...
</form>
About catching form 'onsubmit()'... Never try it. How we can catch the sender this way?

Posted: Wed Jun 20, 2007 5:00 pm
by Weirdan

Code: Select all

<form action="#" id="mainform" method="post"> 
This won't get us at neither to frw.php nor to back.php, though I see you point. As to determining the element to which the event was originally dispatched it what the 'target' event property is for.

Posted: Wed Jun 20, 2007 5:15 pm
by Gente
Sorry. Copied the code but forgot to make change

Code: Select all

<form action="frw.php" id="mainform" method="post">
   ...
  <input type="button" onclick="goto('frw.php');" name="forward" value="Forward" />
  <input type="button" onclick="goto('back.php');" name="back" value="Back" />
  ...
</form>
This should works (at least in FF and IE6).
As to determining the element to which the event was originally dispatched it what the 'target' event property is for.
Thanks. I'll try it