Solved

want to differentiate the button clicks from a single page using javascript.....

Posted on 2003-11-25
12
373 Views
Last Modified: 2008-03-17
Dear experts,

I need to have many buttons in a page and thinking of doing different actions on the individual button clicks.Say for example, i have a search screen from which different criteria options are displayed.For each criteria one button is used to submit the criteria separately.i use single form and all submit buttons.But in the processing servlet i am not able to differentiate the button clicks and the corresponding parameters.i want a solution to solve this issue.somehow i felt javascript would be of my help.But i could not get the correct script for implementing this.Can any expert would heed to help me?
thanks in advance
sat
0
Comment
Question by:sathiyam
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 8

Expert Comment

by:xabi
Comment Utility
Hi torbjornfahlen,

Here goes an example

<html>
<head>
</head>
<body>
<form action="put_here_your_action_page" method="GET" name="myform">
Field1: <input type="text" value="" name="field1">
<br><input type="button" value="Option1" onclick="this.form.action.value='option1';this.form.submit()">
<br><input type="button" value="Option2" onclick="this.form.action.value='option2';this.form.submit()">
<br><input type="button" value="Option3" onclick="this.form.action.value='option3';this.form.submit()">
<input type="hidden" name="action" value="">
</form>
</body>
</html>


xabi
0
 
LVL 8

Expert Comment

by:xabi
Comment Utility
As you can see the info about the burron is in the field "action"

xabi
0
 
LVL 2

Expert Comment

by:DidierD
Comment Utility
You can do it like this

<script>
  function gotoCancel(){
    document.yourForm.action="nextPage1.do";
    document.yourForm.submit();
  }

  function gotoOK(){
    document.yourForm.action="nextPage2.do";
    document.yourForm.submit();
  }
 
  function gotoSomething(param){
    document.yourForm.action="nextPage3.html?param=" + param;
    document.yourForm.submit();
  }

</script>


<input type="button onClick="goToCancel()">
<input type="button onClick="gotoOK()">
<input type="button onClick="gotoSomething(param1)">
<input type="button onClick="gotoSomething(param2)">


Greetz,
Didier
0
 
LVL 10

Expert Comment

by:NetGroove
Comment Utility
Or you let your buttons be Submit buttons, give them all the same name, elt say "Submit" and assign to every button a different value according to requested action.
Like this:

<form action="yourpage.jsp">
<input type=submit name="Submit" value="new">
<input type=submit name="Submit" value="mod">
<input type=submit name="Submit" value="del">
</form>

On server side you check the button like other form elements for its value to see the submitted value: Submit == "mod"

0
 
LVL 8

Expert Comment

by:xabi
Comment Utility
NetGroove, you are right, the only problem is the "lavel" of the button ....  It must be the same as the text to be checked at the server side.

xabi
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Expert Comment

by:jaysolomon
Comment Utility
do not name the submit, or you may run into problems
0
 

Author Comment

by:sathiyam
Comment Utility
hi xabi,
is there a way to use object id of an html element and identify the element and process the click?
sat
0
 
LVL 10

Expert Comment

by:NetGroove
Comment Utility
I named it Submt.
That name provoke NO problems.

0
 
LVL 8

Expert Comment

by:xabi
Comment Utility
This way?

<html>
<head>
<script>
function go(sName) {
  alert("You clicked " + sName + " id button");
  document.myform.action.value = sName;
  document.myform.submit();
}
</script>
</head>
<body>
<form action="put_here_your_action_page" method="GET" name="myform">
Field1: <input type="text" value="" name="field1">
<br><input id="id1" type="button" value="Click for action 1" onclick="go(this.id)">
<br><input id="id2" type="button" value="Click for action 2" onclick="go(this.id)">
<br><input id="id3" type="button" value="Click for action 3" onclick="go(this.id)">
<input type="hidden" name="action" value="">
</form>
</body>
</html>
0
 
LVL 8

Accepted Solution

by:
xabi earned 125 total points
Comment Utility
Even better:

<html>
<head>
<script>
function go(oButton) {
  alert(oButton.form.name);
  alert("You clicked '" + oButton.id + "' id button");
  oButton.form.action.value = oButton.id;
  oButton.form.submit();
}
</script>
</head>
<body>
<form action="put_here_your_action_page" method="GET" name="myform">
Field1: <input type="text" value="" name="field1">
<br><input id="id1" type="button" value="Click for action 1" onclick="go(this)">
<br><input id="id2" type="button" value="Click for action 2" onclick="go(this)">
<br><input id="id3" type="button" value="Click for action 3" onclick="go(this)">
<input type="hidden" name="action" value="">
</form>
</body>
</html>
0
 
LVL 16

Expert Comment

by:jaysolomon
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: xabi {http:#9818207}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now