如何从两个下拉列表中获取价值并将其传递给不同的网址?

问题描述:

I have two dropdown and want to get the selected year value of the first one and add it to the url of the header page and also to the second dropdown url options so that when I select a php page from the second dropdown I can see the year value on the page.

<?php    $id = $_GET['id']; ?>

   <form method="post">
    Report Period:
    <select name="year" id="year"  >
        <option style="display:none;"> Select</option>

        <?php

        $result1 = mysql_query("select year from year_data where id=$id");

        while ($row = mysql_fetch_assoc($result1)) {

            $period= $row['year'];


            echo "<option  value=\"$period\">$period</option>";

        }
        ?>

    </select>

     Type:
    <select name="report"  id="report" > 


    <option style="display:none;">--Select--</option>


        <?php

    echo "<option value=\"".reportA.".php?org_id={$id}&year=\">reportA</option>";
 echo "<option value=\"" . reportB . ".php?org_id={$id}&year=\">report B</option>";

        ?>

    </select>
    <input type=submit name=button method="post"  onClick="getValues()"  value="view" >

</form>
    <SCRIPT language="JavaScript">

    function getValues() {
var year = document.getElementById("year").value;
var report=document.getElementById("report").value;
    window.location.href = 'header.php?year=' +  year.options[year.selectedIndex].value
    window.location.href = report +  year.options[year.selectedIndex].value


       }


    </SCRIPT>

window.location.href call will immediately redirect you referenced page. So 2nd call will not work.

What you need to do is ,

Lets say you have two page page1.php <- say this has the code you have written in the question.

page2.php <- You can get values from the get vars. (See Below )

In page1.php change this two line

window.location.href = 'header.php?year=' + year.options[year.selectedIndex].value

window.location.href = report + year.options[year.selectedIndex].value

To

yearValue = year.options[year.selectedIndex].value;

reportValue = report.options[report.selectedIndex].value;

window.location.href = 'page2.php?year=yearValue&report=reportValue';

In page2

report = $_GET['report']

year = $_GET['year']

Happy Coding...

Were you looking for something like this?

If I correctly understand what you need, there is no need to change your getValues() fn, as those values haven't changed - only the visible portion of the 2nd select.

Is this correct?

jsFiddle here

var yr;

$(document).ready(function() {
    $('#selone').change(function() {
        yr = $(this).val();
        var txt;
        $('#seltwo option').each(function() {
            txt = $(this).text();
            $(this).text(txt + ' - ' + yr);
        });
    });

}); //END document.ready