I am trying to create an automatic file download when a visitor submits a validated opt-in form. I would like to initiate the download using a Javascript function as an onsubmit() event. Here is the code I am working with I am already using the form action= to submit the form data to a database and there is already Javascript in place to validate the form. I just need to add the download function.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <style type="text/css" xml:space="preserve">
  BODY, P,TD{ font-family: Arial,Verdana,Helvetica, sans-serif; font-size: 10pt }
  A{font-family: Arial,Verdana,Helvetica, sans-serif;}
  B {   font-family : Arial, Helvetica, sans-serif; font-size : 12px;font-  
  weight     
 : bold;}
  .error_strings{ font-family:Verdana; font-size:14px; color:#660000;}
  </style>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> 
   </script>
 <script type="text/javascript" 
 src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script language="JavaScript" src="gen_validatorv4.js"
type="text/javascript" xml:space="preserve"></script>
   <script type="text/javascipt">
   var downloadURL = function downloadURL(url) {
  var iframe;
  var hiddenIFrameID = 'hiddenDownloader';
  iframe = document.getElementById(hiddenIFrameID);
   if (iframe === null) {
    iframe = document.createElement('iframe');  
    iframe.id = hiddenIFrameID;
    iframe.style.display = 'none';
    document.body.appendChild(iframe);
   }
  iframe.src = 'http://healthyweightnaturally.com/file-download/download.php?   
   download_file=integrative_nutrition_ebook.pdf';   
  }
   </script>
 </head>
 <body>
<form name="myform" id="myform" method="post"  
   action="https://www.example.com/FormHTML.aspx" onSubmit="return downloadURL(url);"> 
        
     <table cellspacing="2" cellpadding="2" border="0">
    <tr>
      <td align="right">
        First Name
      </td>
      <td>
        <input type="text" name="FirstName" />
      </td>
    </tr>
    <tr>
      <td align="right">
        Last Name
      </td>
      <td>
        <input type="text" name="LastName" />
      </td>
    </tr>
    <tr>
      <td align="right">
        Email *
      </td>
      <td>
        <input type="text" name="Email" />
      </td>
    </tr>
  
    <tr>
      <td align="right"></td>
      <td>
        <div id="myform_errorloc" class="error_strings">
        </div>
      </td>
    </tr>
    <tr>
      <td align="right"></td>
      <td>
        <input style="padding-left:10px;" src="http://healthyweightnaturally.com/file-  
     download/download-button.png" type="image" value="submit" value="Submit" />
      </td>
    </tr>
  </table>
    <div style="visibility:hidden"> 
    <iframe name="ifr1" width="20" height="20" id="hiddenIFrameID"></iframe> 
    </div> 
   </form>
 <script language="JavaScript" type="text/javascript"
    xml:space="preserve">//<![CDATA[
   //You should create the validator only after the definition of the HTML form
   var frmvalidator  = new Validator("myform");
  frmvalidator.EnableOnPageErrorDisplaySingleBox();
  frmvalidator.EnableMsgsTogether();
 frmvalidator.addValidation("FirstName","req","Please enter your First Name");
 frmvalidator.addValidation("FirstName","maxlen=20",    "Max length for FirstName   
 is 20");
 frmvalidator.addValidation("FirstName","alpha_s","Name can contain alphabetic chars    
 only");
 frmvalidator.addValidation("LastName","req","Please enter your Last Name");
 frmvalidator.addValidation("LastName","maxlen=20","For LastName, Max length is 20");
 frmvalidator.addValidation("Email","maxlen=50");
 frmvalidator.addValidation("Email","req");
 frmvalidator.addValidation("Email","email");
 //]]>
 </script>
 </body>
 </html>
I want to use onsubmit instead of onclick because I need to make sure that the form has been validated before the file is able to be downloaded.
I am somewhat new to Javascript and have been struggling with this. I would greatly appreciate someone's assistance. The closest thing I found to an answer can be seen at HTML OnSubmit: Download OR HTML, but since they never gave any code examples it does not help me see the solution.
 
     
     
    