0

Is there a way to call Apex method to save the values in a form element? For example :

<form id="scan-form" action="https://www.huika.com/payment/index.php/starting" method="post"
          target="_blank" charset="UTF-8" enctype="application/x-www-form-urlencoded" onclick="actionFunctionMethod();">


        <input id="zehut" name="zehut" value="1212112" />
        <input id="name" name="name" value="bingo" />
        <input id="email" name="email" value="bingo0@gmail.com" />
        <input id="token" name="token" value="233243455354dfggdf4" />
        <input id="sum" name="sum" value="100" />

        <input type="submit" value="Search" />

    </form>

I tried to use actionFunction - But it has to use inside apex:form..

The reason for not using the apex:form element, is that I can't make a redirection following the Post method for sending the data.

Any suggestion?

Thanks.

  • 1
    Why don't you return a PageReference from the actionFunction method instead? Don't do any rerenders and and your redirection will happen! – Saket Joshi Feb 12 at 11:48
  • @Saket Joshi actionFunction means I would need to replace the form element with apex:form element. I need to make a Post redirection with params from the form - But when using the : pageRef.getParameters().put('id','121212') for example inside the pageRefenrence function - Its pass on the URL as get and not as Post - and this is a problem. Any suggestion? Or Enlightenment? Thanks! – Salvation Feb 12 at 12:01
1

Calling any network operation during a form post is destined to fail, as the browser severs all connections as part of its "unload" event. Instead, you would still want an apex:form, and you'd have a normal form for submitting afterwards. I won't write a full answer, but it would start off like this:

<script>
  function submitForm() {
    document.querySelector("#secondForm").submit();
  }
</script>
<apex:form>
  <apex:input value="{!zehut}" label="Zehut" ... />
  ...
  <apex:commandButton action="{!save}" rerender="hiddenForm" oncomplete="submitForm();" 
     label="Submit" />
</apex:form>
<apex:outputText id="hiddenForm">
  <form id="secondForm">
    <input type="hidden" name="zehut" value="{!zehut}" />
    ...
  </form>
</apex:outputText>

Other alternatives are possible as well, but the main thing is that you need to make sure the first action fully commits before submitting the form.

  • Thanks a lot! @sfdcfox Its exactly what I needed!! But I have one question on it if I may - how can I pass the values to the <input> tag in the form? for example : <input id="zehut" name="zehut" value="{!zehut}" /> because this is the form I need eventaully to be submitted. Thanks!! By the way - is there a way of doing it from Serverside? with PageReference maybe? I didnt find a way to pass the values as a post. Just as a get on the URL.. – Salvation Feb 13 at 7:32
  • 1
    @Salvation pagereference is rather limited, so no. The specific reason why I gave the code as above is because it should work. The order of operations is important here. – sfdcfox Feb 13 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.