Search code examples
javascriptjavajqueryvelocity

How to submit a form with different actions?


I do have a Java Web Application, it does use velocity (*.vm) as the presentation file. I do have a form with a lot of inputs/select box and I want to submit it with different source in the action of the form. The form does have 2 buttons. First named as Submit, its default one, it just submits the form and it does work as it should. I dont have a problem with that. The second button is just a default (save draft) button, and this button should also submit the form but to different source.. I've configured everything from Java side, struts, beans, etc.. but I get only null from Java.. its not submitting the data.

I've tried the following JQuery script, but it didn't help:

function submitDraft(form){
                    var url = "addDraftMarriageAction.action";
                    var formData = {};
                        $(form).find("input[name]").each(function (index, node) {
                            formData[node.name] = node.value;
                        });
                    $.post(url, formData).done(function (data) {
                    alert("Draft has been saved");
                    });
                }

And I called this function from the onClick() method of the second button, it gives the alert but the Java side doesnt get this.

Any suggestions or help is appreciated


Solution

  • I've found a solution, instead of the posted javascript code I used this:

                        function submitDraft(){
                        var url = "addDraftMarriageAction.action";
                        $('form').attr("action", url);  //change the form action
                        $('form').submit();  // submit the form
                    }
    

    And I got all values from the Java side and now everything works. So, I didnt have any problems from the Java but it was more of the javascript problems...

    Credits to Posting same form to different actions depending on button clicked