You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2003/02/02 19:51:22 UTC

DO NOT REPLY [Bug 16679] New: - Newbie FAQ - Why can't my javasript submt a form?

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16679>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16679

Newbie FAQ - Why can't my javasript submt a form?

           Summary: Newbie FAQ - Why can't my javasript submt a form?
           Product: Struts
           Version: Unknown
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Documentation
        AssignedTo: struts-dev@jakarta.apache.org
        ReportedBy: edgar@blue-moose.net


Aah:  But you can use javascript to submit a form.  You can submit a form with 
a link as below.  BTW, the examples below assume you are in an <html:form> 
block and 'myForm' is picked up from the struts-config.xml name field of the 
action.

     <a href='javascript:void(document.forms["myForm"].submit()>My Link</a>

Now the trick in the action is to decode what action you intend to perform.  
Since you are using javascript, you could set a field value and look for it in 
the request or in the form.

... html/javascript part ...

    <input type='hidden' value='myAction' />
    <input type='button' value='Save Meeeee'
        onclick='document.forms["myForm"].myAction.value="save";
                 document.forms["myForm"].submit();' />
    <input type='button' value='Delete Meeeee'
        onclick='document.forms["myForm"].myAction.value="delete";
                 document.forms["myForm"].submit();' />

... the java part ...

    class MyAction extends ActionForm implements Serializable {

        public ActionForward execute (ActionMapping map, ActionForm form,
            HttpServletRequest req, HttpServletResponse) {

                String myAction = req.getParameter("myAction");

                if (myAction.equals("save") {
                       ...  save action  ...
                } else if (myAction.equals("delete") {
                       ...  delete action  ...
                }
            }
        }
    }

This is just one of many ways to achieve submitting a form and decoding the 
intended action.  Once you get used to the framework you will find other ways 
that make more sense for your coding style and requirements.  Just remember 
this example is completely non-functional without javascript.  Here is a link 
which utilizes the LookupDispatch action to submit forms with multiple actions 
without javascript

    http://husted.com/struts/tips/003.html

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org