JS Submit

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

JS Submit

Post 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?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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>
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Re: JS Submit

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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
Post Reply