Note that there are some explanatory texts on larger screens.

plurals
  1. POForce Download of .mp3 or .zip files on Ajax success
    primarykey
    data
    text
    <p>After a user fills out an email verification form (which is submitted via AJAX), I want to force a download to start in the current browser window if the AJAX call response is successful.</p> <p>The email verification form looks like: </p> <pre><code>&lt;form id="form_download_email" method="POST" action="music_email_verification.php"&gt; &lt;input id="download_email" type="text" tabindex="1" /&gt; &lt;br/&gt; &lt;a href="javascript:void(0);" id="download_email_button"&gt; Download! &lt;/a&gt; &lt;/form&gt; </code></pre> <p>The js/jquery once the "download_email_button" is clicked looks like this:</p> <pre><code>$("#download_email_button").click(function() { var action = $("#form_download_email").attr('action'); var form_data = {email: $("#download_email").val()}; $.ajax({ type: "POST", url: action, data: form_data, success: function(response){ if(response == 'success'){ window.location.assign('URL to .mp3 or ZIP'); } } }); }); </code></pre> <p>The response=='success' block gets entered, and for now the only solution I found to force a download of a file without launching a pop up window (through window.open - which gets blocked by Chrome anyway), is to use window.location.assign as shown above. Is this the correct way of going about forcing a file download through JS??</p> <p>However, using the window.location.assign method I can only download 1 file, and if I try to put in two lines with two mp3s, it doesn't work. AKA:</p> <pre><code>window.location.assign('http://song1.mp3'); window.location.assign('http://song2.mp3'); </code></pre> <p>I do not want the user to have to click on another link after submitting the form to have to download the .mp3/.zip, I want to try to do it all in JS/jQuery.</p> <p>The final method I thought about was on response=='success' to have the browser click on two hidden tags on the document that had their href pointing to the mp3s. However, it seems that "The issue is that you can't trigger a link's default action via a click" with jquery, just the onclick attribute.</p> <p>Should I be doing this with js? If there is no way to do this, should I try something like PHP?</p> <p>Any suggestions on the current way I do it or recommendations on how to download multiple mp3 files would be helpful!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload