This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Submit a form via hyperlink

 
Susan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Susan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Merrill,

I tried that but it didnt fix the problem?


thanks,

Susan
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed something else: Try changing onklick to onclick.
 
Susan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Chen
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Ram, yes, <a href="javascript:submitForm()">SUBMIT</a> works for me.

Thanks a lot
David
 
Robertis Tongbram
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);">
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic