2013-09-24 31 views
0

我有兩個下拉菜單,並希望獲得第一個選定的年份值,並將其添加到頁眉的網址以及第二個下拉網址選項,以便當我從第二個下拉菜單中選擇一個php頁面時,我可以查看頁面上的年份值。如何從兩個下拉列表中獲取值並將其傳遞給不同的網址?

<?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> 
+2

爲什麼不提交表單? –

+0

將表單的動作設置爲下一頁,然後您可以使用'$ _POST'從下一頁訪問表單值。 – Jasper

+0

你沒問題在答案中使用jQuery? – gibberish

回答

1

window.location.href通話將立即重定向你引用的頁面。所以第二個電話不會工作。

你需要做的是,

比方說你有兩個頁面 page1.php中< - 說這有你寫在問題的代碼。

page2.php < - 您可以從get vars中獲取值。 (見下文)

在變化page1.php中這兩個線路

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

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

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

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

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

在第2頁

report = $_GET['report']

year = $_GET['year']

ħ appy編碼...

+0

感謝您的幫助。我試過你的代碼,我可以得到年份和報告值,但重定向部分不工作,我可以將page2顯示爲彈出窗口嗎? – user2812642

+0

你必須提供完整的絕對URL,如果在本地機器,然後像「http://localhost/your_project_path/page2.php ...」或在一個活的域名,然後「http://domain.com/。 ../page2.php」。 – Ibrahim

0

你是否在尋找類似的東西?

如果我正確理解你需要什麼,沒有必要改變你的getValues()fn,因爲這些值沒有改變 - 只有第二選擇的可見部分。

這是正確的嗎?

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 
+0

我應該在getValue()函數中使用它嗎?它實際上沒有工作! – user2812642

相關問題