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
    }
}
2
  • Why do you want to not use apex:form?
    – sfdcfox
    Commented Jan 14, 2018 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
    Commented Jan 14, 2018 at 14:57

1 Answer 1

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>
2
  • That's a fair solution indeed
    – Julio
    Commented Jan 22, 2018 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. Commented Jan 23, 2018 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .