There is a form on which buttons (links) for downloading are located, as well as three drop-down lists: "Department", "Month", "Year". It is necessary, provided that certain values ​​are selected from the drop-down lists, so that when you click on the button, the file from the server that matches the selected conditions in the lists is downloaded.

Figuratively speaking, the link should be: http://ip-adress:port/statistika/dolgi/КОД_ОТДЕЛА/mm/su/КОД_ГОДА/КОД_МЕСЯЦА/file.txt , where the КОД ОТДЕЛА , МЕСЯЦА , ГОДА is the value codes from our drop-down lists .

How to create and describe the variable of each list correctly, and then insert it into its place in the link code so that when the variable values ​​change, the file we need is downloaded?

  • I am ready to thank more materially - with this to you for freelancing, here is a different philosophy . - Alex

2 answers 2

In the onchange event of the lists, generate the required URL and set it to the href attribute

 jQuery(document).ready(function($) { $("select").change(function() { var dep = $("#department").val(); var month = $("#month").val(); var year = $("#year").val(); if (dep === null || month === null || year === null) $("#download").hide(); else $("#download").attr({ href: 'http://myserver/dep-' + dep + '/month-' + month + '/year-' + year + '/myfile.txt' }).show(); }); $("#download").hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="department"> <option disabled="disabled">Выберите отдел</option> <option value="Код отдела 1">Отдел 1</option> <option value="Код отдела 2">Отдел 2</option> <option value="Код отдела 3">Отдел 3</option> </select> <select id="month"> <option disabled="disabled">Выберите месяц</option> <option value="Код месяца 1">Месяц 1</option> <option value="Код месяца 2">Месяц 2</option> <option value="Код месяца 3">Месяц 3</option> </select> <select id="year"> <option disabled="disabled">Выберите год</option> <option value="Код года 1">Год 1</option> <option value="Код года 2">Год 2</option> <option value="Код года 3">Год 3</option> </select> <p><a id="download" href="">Скачать</a></p> 

  • I just can not understand why your script works here, but I don’t want it in any way - it does not form a link at all. As if the Java script does not work, as if it does not exist! And neither in its pure form, as it goes with you, nor applicable, coupled with my code - in any way! (( - Oleg Kasumoff
  • @OlegKasumoff And are you connecting the jQuery library? - Anton Shchyrov
  <form action="select.php" method="post"> <p><select size="2" name="department"> <option disabled>Выберите отдел</option> <option value="Код отдела 1">Отдел 1</option> <option selected value="Код отдела 2">Отдел 2</option> <option value="Код отдела 3">Отдел 3</option> </select></p> <p><select size="2" name="month"> <option disabled>Выберите месяц</option> <option value="Код месяца 1">Месяц 1</option> <option selected value="Код месяца 2">Месяц 2</option> <option value="Код месяца 3">Месяц 3</option> </select></p> <p><select size="2" name="year"> <option disabled>Выберите год</option> <option value="Код года 1">Год 1</option> <option selected value="Код года 2">Год 2</option> <option value="Код года 3">Год 3</option> </select></p> <p><input type="submit" value="Отправить"></p> </form> 

As a result, in PHP you can extract from the $ _POST variable your department, month and year codes, and then collect your link. Although it would be more convenient to store all your files in one directory, and write all the same information to the database and bind a link to the file to it