2012-12-10 108 views
2

我試圖創建一個自動文件下載,當訪問者提交驗證的選擇參加表格時。我想用一個javascript函數作爲onsubmit()事件來啓動下載。這裏是我正在使用的代碼我已經在使用表單action =將表單數據提交到數據庫,並且它們已經是javascript來驗證表單。我只需要添加下載功能。使用Javascript&onSubmit自動下載文件

<?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.mysite.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> 

我想使用onsubmit而不是onclick,因爲我需要確保表單已被驗證,然後才能下載文件。 我對JavaScript有點新,並且一直在爲此掙扎數日。我將不勝感激別人的幫助。我發現最接近的答案是HTML OnSubmit: Download OR HTML,但由於他們從未給出任何代碼示例,因此我無法看到解決方案。 非常感謝您的幫助。對此,我真的非常感激!

回答