1

I have an html <form> I would like to use to trigger an apex function without using any <apex:form> on the page. Is that possible? If so how?

Code sample:

Page:

<apex:page standardController="SomeObj__c" extensions="myProcessor">
   <form action="send" method="post">
      <!-- some fields with values to send over -->
   </form> 
</apex:page>


Controller:

public with sharing class myProcessor {

    public myProcessor(ApexPages.StandardController controller) {
    }

    public PageReference send() {
        // processing
    }
}
  • Why do you want to not use apex:form? – sfdcfox Jan 14 '18 at 14:46
  • On reason is that the apex:form with forceSSL="true" cannot have its action url processed by a URL Rewriter class. I would expect to control the behavior using an HTML form instead – Julio Jan 14 '18 at 14:57
1

I'm thinking you'll need to use Javascript Remoting since apex:actionFunction has to be in an apex:form. However, since it uses asynchronous ajax, you'll need to prevent submitting the form until the callback function:

New Controller method:

@remoteMethod
public static string send()
{
    return '';
}

Visualforce:

<script type="text/javascript">
    function doSubmit(form)
    {
        myProcessor.send(function(result, event){
                form.target = result;
                form.submit();
        });
        return !!form.target; //Prevent the first submit
    }
</script>
<form action="send" method="post" onsubmit="return doSubmit(this);">
  <!-- some fields with values to send over -->
</form>
  • That's a fair solution indeed – Julio Jan 22 '18 at 7:50
  • 1
    @Julio As an aside, if you are using a standard html element like a button to submit the form, you could have its onclick function always return false and call the remoting function above, so you wouldn't need the weird return !!form.target line. – IllusiveBrian Jan 23 '18 at 12:31

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.