How to force the download of a pdf?

2

I have a PHP and HTML application of a registration form, how do I download a select as an example: Clicking on yes, it downloads according to a link that does this (download) in pdf format. Follow the print for more information and the script as well. '

<script>
  function carregaPdf(){
     if(document.getElementById("alojamento").value == "SIM"){
        window.open("http://www.ibc.gov.br/media/common/regulamento_interno_do_alojamento_ibc.pdf");
      }
   }
</script>

<div class="form-group">
    <label class="col-sm-2 control-label">Alojamento</label>
    <div class="col-sm-10">
        <select onchange="carregaPdf()" class="form-control" name="alojamento" id="alojamento" style="width:100px; padding:2px;" required>
            <option value="">Selecione</option>
            <option value="SIM">SIM</option>
            <option value="NAO">NÃO</option>
        </select>
    </div>
</div>

My doubt is that you are opening a window with the pdf, I want you to force the download of the pdf file as soon as you click on yes, can anyone help me?

    
asked by anonymous 19.12.2016 / 11:43

1 answer

0

You can create a change function to receive the value of the combobox and do an option check. If you choose yes, it redirects the user to the file link.

$(document).ready(function(){
   $('select').on('change', function(){
      var valor = $(this).val();
     
      if(valor == 1){
         document.location.href = 'link-para-download.php'; // ou .pdf 
      }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select><optionvalue="0">Selecione a opção</option>
  <option value="1">Sim</option>
  <option value="2">Não</option>
</select>
    
19.12.2016 / 11:48