0

I need to send a pdf file to user immediately after user click submit form button. Plugin use Ajax send form so page is not refresh. I don't have an advanced knowledge in programming, and I didn't find right solution for now. What I think that I can create link and at the same time do click and file will start download. But I don't know the right way how to do it. What I think I can do is:

$('form input[type="submit"]').click(function(){
$('form input[type="submit"]').after('<a href="http://www.mywebsite.ru/bitrix/myfile.pdf" class="download-file"></a>');
$('.download-file').click();
})

But I'm not shure will this work, and is it true way send file to user by this way after form submit. So I need some body to explain and may be some example, what is the easiest way to send file to user after form submit. Also I have some knowledge in jQuery, but my Ajax knowledge very bad :(

sanchahous
  • 181
  • 6
  • 17

1 Answers1

1

I would return the PDF url from server after submitted data validation. Therefore, you could handle this URL and perform downloading with visual feedback user to explain why a file is downloading.

$(function(){
    $.ajax({
       url : 'server.php',
       type : 'POST',
       data: {field1: 1, field2: 2},
       success : function(html, statut){
           //Visual user feedback
           alert('A PDF file will be automatically downloaded');

           //Download file
           window.location.href = html;
       }
       error : function(result, statut, error){
           alert('something wrong just happened!');
       }
    });
});

You can also replace window.location.href = html; by better suggestions you will find here : Download File Using Javascript/jQuery

sdespont
  • 13,915
  • 9
  • 56
  • 97