File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Submit a form via hyperlink Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Submit a form via hyperlink" Watch "Submit a form via hyperlink" New topic
Author

Submit a form via hyperlink

Susan
Ranch Hand

Joined: Apr 26, 2005
Posts: 54
Hi,

I'm trying to use javascript to submit a form when a hyperlink is clicked. However, in the struts action (v1.2.4) when I retrieve the values from the form they are empty? Whereas, when I click the submit button it works fine.

The code is quite simple, the JSP being:
<SCRIPT LANGUAGE="JavaScript" SRC="http://localhost:8080/strutsAndTiles/scripts/Test.js"></SCRIPT>
...
<html:form action="/postForm">
...
<html:text property="breed" size="20" maxlength="20"/>
<html:link action="/postForm" onklick="submitIt()">post</html:link>
...
<html:submit property="submit">submit</html:submit>
</html:form>


The javascript in Test.js is:
function submitIt() {
var breed = document.myForm.breed.value;
alert(breed); // displays the value in the dialog
document.myForm.method="POST";
document.myForm.submit();
}


struts-config is:
<form-bean name="myForm" type="org.apache.struts.validator.DynaValidatorForm" >
<form-property type="java.lang.String" name="breed"/>
</form-bean>
...
<action path="/postForm" type="PostFormAction" name="myForm" validate="false" input="enterPage" scope="request" >
<forward name="success" path="welcomePage" />
</action>


and the PostFormAction is:
public ActionForward execute(...) {
String breed = (String)PropertyUtils.getSimpleProperty(form, "breed");
System.out.println("breed=" + breed); // this displays 'breed='
return mapping.findForward("success");
}

Any ideas are appreciated?


Susan
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I've run into a similar problem before. If you name your submit button "submit", javaScript thinks you're referring to this button when you try to execute document.myForm.submit(). Try naming your submit button something other than "submit" and see if it works.

This problem has nothing to do with struts. It really belongs in the html/javascript forum.


Merrill
Consultant, Sima Solutions
Susan
Ranch Hand

Joined: Apr 26, 2005
Posts: 54
Thanks Merrill,

I tried that but it didnt fix the problem?


thanks,

Susan
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I just noticed something else: Try changing onklick to onclick.
Susan
Ranch Hand

Joined: Apr 26, 2005
Posts: 54
Merrill,

you cant post to this forum with the term 'onclick' in your message, so common alternatives are 'on click' or 'onklick', etc.


thanks again,

Susan
David Chen
Ranch Hand

Joined: Jan 06, 2002
Posts: 57
Hi, Susan:

I met exactly the same problem as you posted, Did you figure out how to populate form fields values by using hyperlink? Or does anyone can please help on that?

Thanks a lot
David
Ram Munshi
Ranch Hand

Joined: Aug 09, 2005
Posts: 55
Instead of using html:link ,you can use html <a href >

<html:link action="/postForm" onklick="submitIt()">post</html:link>
,in the href you ,you can give your function name that is submit it.
e.g.

<a href="submitit()" ></a>

I think this would solve the problem,only thing is that you can not open it in new window.


Ram Munshi
David Chen
Ranch Hand

Joined: Jan 06, 2002
Posts: 57
Thank you very much Ram, yes, <a href="javascript:submitForm()">SUBMIT</a> works for me.

Thanks a lot
David
Robertis Tongbram
Greenhorn

Joined: Jun 14, 2004
Posts: 4
If u still want to open a new window then, in your function that submits the form, do a window.open() and then form.submit() for submitting the form
. I hope this helps..




function submitNopenNew(form)

{
var wt = 800;
var ht = 600;

var wn = "scrollbars,menubar,status,screenX=0,screenY=0,top=0,left=0,width=" + wt + ",height=" + ht;

window.open('','targetName',wn);

form.submit();
}

<form name="myForm" target="targetName" ...>

...


<a href="javascript:nWindow=submitNopenNew(document.myForm);">
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Submit a form via hyperlink