Solved

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

Posted on 2003-11-25
12
376 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
ID: 9818040
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
ID: 9818054
As you can see the info about the burron is in the field "action"

xabi
0
 
LVL 2

Expert Comment

by:DidierD
ID: 9818070
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 10

Expert Comment

by:NetGroove
ID: 9818080
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
ID: 9818098
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
 
LVL 16

Expert Comment

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

Author Comment

by:sathiyam
ID: 9818127
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
ID: 9818159
I named it Submt.
That name provoke NO problems.

0
 
LVL 8

Expert Comment

by:xabi
ID: 9818181
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
ID: 9818207
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
ID: 10383703
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how would you interpret lines 3 36
Wrapper for APPs 9 76
Show/Hide ASP.NET controls with JQuery best practices 3 45
Using jQuery to set a DIV attribute on page load 9 29
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

813 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

11 Experts available now in Live!

Get 1:1 Help Now