Setting the value of a hidden variable on button click and then Submit the Form using .submit() jquery struts

StackOverflow https://stackoverflow.com/questions/22397091

Question

I have a problem with Form Submit in struts. Setting the value of a hidden variable on button click and then Submit the Form using .submit() jquery is not allowing to show the sucess message in the same page in struts.

when the button is clicked, before the form is submited I need to set a hidden variable from the click of the button and then the form is to be submitted. I am able to submit the form using the below code but not in the same popup.

<div id="formResult" >
  <s:form id="formId" name="form">
    <s:hidden id="actionId" name="action"></s:hidden>
    <table cellpadding="0" cellspacing="0" border="0" width="90%" >
      <s:if test ="approve">
        <tr>
          <td>File is Approved!!</td>
          <td><td>
        </tr>
      </s:if>           
      <s:elseif test ="rejectFile">
        <tr>
          <td>File is Rejected!</td>
          <td><td>
        </tr>
      </s:elseif>           
      <s:else>
        <tr>
          <td width="30%"><b><s:text name="idLabel" /></b></td>
          <td width="40%"><s:property value="Id"/></td>
        </tr>
        <tr>
          <td><div style="height:5px"></div></td>
          <td><div style="height:5px"></div></td>
        </tr>
        <tr>
      </s:else>
      <tr>
        <td width="90%" colspan="2" align="right">
          <s:if test="!approveFileSuccess">
            <sj:a 
              id="approvefileId" 
              onclick="approveFunction();"                      
              button="true"
              targets="formResult">
              Approve
            </sj:a>
          </s:if>                       
          <s:if test="!rejectFileSuccess">
            <sj:a 
              id="rejectfileId" 
              onclick="rejectFunction();"               
              targets="formResult" 
              button="true">
              Reject
            </sj:a>
          </s:if>
    </table>
  </s:form>
</div>

JQUERY:

function approveFunction(){
  $('#actionId').val("Approve");
  document.formId.submit();
}

function rejectFunction(){
  $('#actionId').val("Reject");
  document.formId.submit();
}

When document.formId.submit(); is clicked form submit takes the popup to another page. Not able to show success in the same page.

Was it helpful?

Solution

Finally I got it right for the same code given above with a few changes. Without Ajax it can be done..!! Use onClickTopics which does the work before the form is submitted and also without refreshing it :)

JSP

<sj:a 
id="approvefileId" 
formIds="approvefileform"                       
button="true"
targets="formResult"
onClickTopics="beforeApprove">
Approve
</sj:a>

JQUERY

$.subscribe('beforeApprove', function(event,data) { //before form submit on click functionality 
$('#actionApproveRejectId').val("Approve"); //to set the hidden variable
});

Its Working Perfect!!!!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top