2013-08-01 90 views
0

我在使用我的网站URL传递变量时遇到问题。在URL中传递php变量

下面的代码:

function sort(form) { 
     var Page = "?"; 

     var iweek = form.listWeeks.SelectedIndex; 
     var week = form.listWeeks.options[iweek].value; 

     var month = form.listMonth.selectedIndex+1; 

     var iyear = form.listYear.selectedIndex; 
     var year = form.listYear.options[iyear].value; 

     var URL = Page + "week=" + week + "&month=" + month + "&year=" + year; 

     window.location = URL; 

     return false; 
    } 

当我点击提交按钮至极指此功能,网址更改为:

http://localhost/test.php?listWeeks=1&listMonth=August&listYear=2010&Submit=Select 

但我想将URL更改为:

http://localhost/test.php?week=1&month=8&year=2010 

奇怪的部分是,当我将代码更改为:

function sort(form) { 

     var Page = "?"; 

     //var iweek = form.listWeeks.SelectedIndex; 
     //var week = form.listWeeks.options[iweek].value; 

     var month = form.listMonth.selectedIndex+1; 

     var iyear = form.listYear.selectedIndex; 
     var year = form.listYear.options[iyear].value; 

     var URL = Page + "month=" + month + "&year=" + year; 

     window.location = URL; 

     return false; 
    } 

它的工作..任何人都可以告诉我什么问题可能是什么?

谢谢!

+1

这看起来很像JavaScript而不是PHP代码 –

+0

@GeraldSchneider我也注意到了,但是在我看来JavaScript并不是必需的 – Voitcus

回答

1

您可能需要在每个<option>标记中使用value属性。例如

<select name="listMonth"> 
    <option value="1">January</option> 
    <option value="2">February</option> 
    ... 
</select> 

您还可以更改<select name="listMonth"><select name="month">

这应该按预期工作(UPDATE):不需要

<form method="get" action="test.php"> 
    <select name="month"> 
    <option value="1">January</option> 
    <option value="2">February</option> 
    ... 
    </select> 
    <input type="submit" /> 
</form> 

然后JavaScript代码。

+0

我在每个选项标签中都使用了value属性。 当我将listMonth更改为月份时,它返回8月份,我希望它是8。 – BjornBogers

+0

因此,当我更新了答案 – Voitcus

+0

时,只需更改'