title: Javascript in VisualForce page - form submit success, trigger apex?
body: <p>Right now, the code below upon submit, first inserts a file THEN submits the form. This is problematic - I need for it to verify the form was successfully posted to Amazon S3 then insert the  (token) file so it can link to it.</p>
  
  <p>I basically need to reverse SubmitFile and InsertFile, but I'm not sure how to have (If form successfully sent, then run another javascript function). See very bottom for an attempt...Is this possible?</p>
  
  <pre><code>   &lt;apex:pageMessages id="pageErrors"&gt;&lt;/apex:pageMessages&gt;
  &lt;form name="s3Form" action="https://s3.amazonaws.com/mybucket" method="post" enctype="multipart/form-data"&gt;   
      &lt;input type="hidden" name="key"/&gt; 
      &lt;input type="hidden" name="AWSAccessKeyId" value="{!key}"/&gt; 
      &lt;input type="hidden" name="policy" value="{!policy}"/&gt;
      &lt;input type="hidden" name="signature" value="{!signedPolicy}"/&gt; 
      &lt;input type="hidden" name="acl" value="private"/&gt; 
      &lt;input type="hidden" name="x-amz-meta-FileId" value="{!File__c.id}"/&gt;
      &lt;input type="hidden" name="x-amz-meta-OrderId" value="{!OrderId}"/&gt;
      &lt;input type="hidden" name="x-amz-meta-CustomerId" value="{!CustomerId}"/&gt;     
      &lt;input type="hidden" name="success_action_redirect" value="{!serverUrl}{!OrderId}"/&gt;           
  
      &lt;apex:pageBlock title="New File Upload" mode="maindetail" tabStyle="File__c"&gt; &lt;!--  rendered="{!open}"--&gt;
          &lt;apex:pageBlockSection title="File Information" columns="2" collapsible="false" showHeader="false"&gt;                               
              &lt;apex:pageBlockSectionItem &gt;
                  &lt;apex:outputLabel value="Select File" for="selectFile"/&gt;            
                  &lt;input id="selectedFile" type="file" size="25" name="file" onChange="setFileName(this.value)"/&gt;                
              &lt;/apex:pageBlockSectionItem&gt;                                         
          &lt;/apex:pageBlockSection&gt;
          &lt;apex:pageBlockButtons location="bottom"&gt;
              &lt;input class="btn" type="button" value="Upload File" onClick="checkFile();return false;"/&gt;                
              &lt;input class="btn" type="button" value="Cancel" onClick="cancelFile();return false;"/&gt;
              &lt;input class="btn" type="button" value="Complete Order" onClick="completeFile();return false;"/&gt;
          &lt;/apex:pageBlockButtons&gt;          
      &lt;/apex:pageBlock&gt;
      &lt;apex:outputText styleClass="footer" value="Please click Complete Order when all the required files are uploaded. Thank you." /&gt;
  &lt;/form&gt; 
  
   &lt;apex:form id="sfForm"&gt;&lt;!--  rendered="{!open}"--&gt;
  
      &lt;apex:inputHidden id="hiddenServerURL"  value="{!serverURL}"/&gt;
      &lt;apex:inputHidden id="fileName" value="{!fileName}"/&gt;
      &lt;apex:inputHidden id="contentType" value="{!contentType}"/&gt;
      &lt;apex:inputHidden id="fileType" value="{!fileType}"/&gt;
      &lt;apex:actionFunction name="insertFile" action="{!insertFile}" oncomplete="submitFile();return false;"/&gt;
      &lt;apex:actionFunction name="completeOrder" action="{!completeOrder}"/&gt;
  
  
      &lt;script type="text/javascript"&gt;
      var sendFile = false;
      document.getElementById('{!$Component.hiddenServerURL}').value = '{!$Api.Enterprise_Server_URL_140}';       
      function setFileName(file) {
          var f = file.replace(/\\/g, "");
          f = f.replace(/C\:fakepath/g, ""); &lt;!--Required for IE8--&gt;
          document.s3Form.key.value = "{!CustomerName}/{!OrderName}/" + f;
          document.getElementById('{!$Component.fileName}').value = f;
          suffix = f.lastIndexOf(".") + 1;
          contentType = f.slice(suffix);
          document.getElementById('{!$Component.contentType}').value = contentType;
      }
      function setFileType(type) {
          document.getElementById('{!$Component.fileType}').value = type;
      }
      function checkFile() {
          if (document.s3Form.file.value=="") {
              alert("Please, select a file.");
              return false;
          } 
          else if (document.s3Form.fType.value=="--None--") {
              alert("Please, select a file type.");
              return false;
          }
          else {     
              alert("Uploading...Please click OK and wait for page to refresh.");        
              insertFile();
              sendFile = true;
          }
      }
      function submitFile() {
          if(sendFile = false) {
          return false;
          }
          else {
          document.s3Form.submit();
          }
      } 
      function completeFile() {
          completeOrder();
      }   
      &lt;/script&gt;
  
  &lt;/apex:form&gt;
  </code></pre>
  
  <p>In the controller (extension):</p>
  
  <pre><code>//SF File insert on an object (passed from page)
  public PageReference insertFile() {
      this.file.Name = fileName;
      this.file.Type__c = fileType;
      this.file.Content__c = contentType;
      insert this.file;
      return null;
  }
  </code></pre>
  
  <p>Here is the javascript section i tried to alter, but i can't find anything after extensive searches on if this works...</p>
  
  <pre><code>        function checkFile() {
          if (document.s3Form.file.value=="") {
              alert("Please, select a file.");
              return false;
          } 
          else if (document.s3Form.fType.value=="--None--") {
              alert("Please, select a file type.");
              return false;
          }
          else {     
              alert("Uploading...Please click OK and wait for page to refresh.");        
              document.s3Form.submit({
                  success: function(){
                      alert("Testing!");
                      insertFile();
                  },
              });
          }
      }
  </code></pre>