0

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

Daler
  • 1,205
  • 3
  • 18
  • 39
  • Is page being unloaded after button click ? – Rayon Jun 28 '16 at 04:23
  • What you can do is send an additional parameter on button click, identifying the button that you have clicked. And based on the value of the parameter you can either submit the data or create a draft. – Ajit Kumar Singh Jun 28 '16 at 04:28
  • @Rayon, no the page remains the same. it does show the alert message and the Eclipse console throws null pointer exceptions. – Daler Jun 28 '16 at 04:29
  • @AjitKumarSingh, I can't do it. Because the logic behind the submit is different and the logic behind the saveDraft is different. In this case I would have to add a lot of changes to the code on the backend side and front end (javascripts).. so my primary goal is to do not do it like you said. This solution would be as the last possible thing. – Daler Jun 28 '16 at 04:31
  • are you stopping event propagation or default behaviour for the form? – derp Jun 28 '16 at 04:33
  • null pointer exception usually means that it is not getting any value. Another solution I can give is to use button instead of submit. And you can submit the form in javascript for save functionality or make an ajax call for draft. – Ajit Kumar Singh Jun 28 '16 at 04:36
  • @derp, I'm not doing anything else. I just call that JavaScript code from the saveDarft button. I've to somehow simulate the same behavior that the submit type does but for different source in action. – Daler Jun 28 '16 at 04:36
  • @AjitKumarSingh, OP is getting alert and as commented earlier, page is not getting reloaded... – Rayon Jun 28 '16 at 04:39
  • @AjitKumarSingh, your last suggestion is not useful. – Daler Jun 28 '16 at 04:40
  • @Daler Thanks for pointing that out. Anyway finding the reason behind 'null pointer exception' is important. Check if your ajax url is correct, cauz your alert function executes irrespective of success or failure of your ajax call. Check for the data, that you are sending, etc. – Ajit Kumar Singh Jun 28 '16 at 04:48
  • @AjitKumarSingh, I know the reason behind null pointer exception. As I said in my OP it's because the data is not sending to java side. But why it's not sending to java side is my original question, that's why I posted a question and u r suggesting me to look for it.. lol u made my day. Ajax url is correct, because as i said Java side throws an exception.. – Daler Jun 28 '16 at 04:53
  • @Daler, Then it is more of a `Java` question then.. Do post Java code instead of JS... – Rayon Jun 28 '16 at 04:55
  • 1
    Have you tried `console.log(formData)` just before `$.post()` to ensure it has the expected fields? Have you used the browser's Dev Tools network tab to double-check what is in the Ajax request? Have you fixed the Java side to test for missing fields and tell you which one(s) fail(s)? – nnnnnn Jun 28 '16 at 04:57
  • @nnnnnn, I've found a solution. Thx for your suggestions. I'll post it right away. – Daler Jun 28 '16 at 05:04

1 Answers1

1

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

Community
  • 1
  • 1
Daler
  • 1,205
  • 3
  • 18
  • 39