File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes call Action from javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "call Action from javascript" Watch "call Action from javascript" New topic
Author

call Action from javascript

natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
Hi all!
i would like some help with what i am trying to do.i am making my project using struts and jsp.in my jsp for user registering,i use validation.xml for checking that user inputs corrrect data.but i am trying to write a javascript function which checks if user has selected the same values in more than one of the 3 selects i have in the page.if that is the case i would like a message to appear and let the user change his choice.unfortunately i cannnot do that
the problem is that the Action takes control and the user doesnt have the chance to change his input...any ideas ???thanks!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You using onsubmit?



Eric
natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
my code is
<script type="text/javascript">
function checkUniquePrefs() {

var test = document.getElementById("myform")

var list = document.getElementById("select1")
var name = list.options[list.selectedIndex].text

var list2 = document.getElementById("select2")
var name2 = list2.options[list2.selectedIndex].text
var list3 = document.getElementById("select3")
var name3 = list3.options[list3.selectedIndex].text


if (name != name2 && name != name3 && name2 != name3) {
test.submit();

} else {
alert("alert");
}

}
</script>
what is wrong with that code?
natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
i have tried it using onsubmit and also on click but in neither cases does this work..any thoughts?thanks!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
if you are uswing onsubmit than you need to return true or false

if (name != name2 && name != name3 && name2 != name3) {
return true;

} else {
alert("alert");
return false;
}
natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
this function is driving me crazy!
it does submit even though i return false in case of mistake!!!
the Action takes the control and i can figure out why!!!
i have tested calling the function from an input type of submit,from a simple button and from the form onsubmit but it never works!it always submits!!!why?any thoughts??thanks!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
If it does not work, than you have a JavaScript error in your code:

Basic working code here:


Eric
Ram Gokul
Ranch Hand

Joined: Oct 07, 2005
Posts: 85
can you put entire code here ..I will try to submit to my struts action.

the code you gave works for me ...but I am submitting to http://www.javaranch.com rather than a struts action. And when it run across error it DOES NOT submit...
natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
i cant believe my eyes!!it works!!!finally!thank you soooo much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: call Action from javascript
 
Similar Threads
Action from javascript
Dynamically Mapping Form Action with javascript
javascript with struts and jsp..
Configuring the servlet for https://
Changing variables dynamically