0

I have two separate fields with a drop-down list, one is for User and the second Data. Then I have a button, which would ideally work as an output of the selection of the two drop-down lists. The form should retrieve the file that is relevant to the selection. The file to be opened has always the following format so I was hoping to make use of that: _.csv

Here's the simplified version of what I'm using:

<td align="left"><!-- User/data selector -->
  <table border="0">
    <tbody>
      <tr>
        <th align="left">User:</th>
        <td>
        <select name="users" id="users">
        <option value="user1">User 1</option>
        <option value="user2">User 2</option>
        <option value="user3">User 3</option>
        </select>
        </td>
      </tr>
      <tr>
        <th align="left">Data:</th>
        <td>
        <select name="data" id="data">
        <option value="data1">Data 1</option>
        <option value="data2">Data 2</option>
        <option value="data3">Data 3</option>                    
        </select>
        </td>
      </tr>
      <tr>
        <td></td>
        <td><input type='button' value='Generate Report' onClick='newWindow();'/></td>
      </tr>
    </tbody>
  </table>

So, what I'm struggling with is to know what to write in the function newWindow() to get the needed 'User' and 'Data' selection to get the correct file. Can you help?

Much appreciated!

Xavi López
  • 27,550
  • 11
  • 97
  • 161
janecs
  • 199
  • 1
  • 2
  • 9

1 Answers1

0

You could get the values of those <select> fields with document.getElementById(id).value, where id would be data or users, in order to make up the csv file name.

The openWindow function could look like:

function openWindow(){
    var users = document.getElementById('users').value;
    var data = document.getElementById('data').value
    var fileName =  users + "_" + data + ".csv";
    var pathToFiles = "/whateverdirectory/";

    window.open(pathToFiles + fileName, fileName);
}

Also, remember you shouldn't be using <table>s for layout, they're meant for displaying tabular data. It is recommended to use <div>s instead.

You might also find this blog entry useful: Using the window.open method.

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161