在URL中传递php变量

时间:2013-08-01 11:47:20

标签: php javascript url variables

我在使用我网站的网址传递变量时遇到了问题。

以下是代码:

    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

但我想将网址改为:

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;
    }

它有效..有谁能告诉我可能是什么问题?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

您也可以将<select name="listMonth">更改为<select name="month">

这应该按预期工作(更新):

<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代码。

答案 1 :(得分:0)

您注释掉的两行代码中的某些内容会引发错误。如果没有看到更多的上下文(例如表单),为什么不能说这个。

这导致JavaScript崩溃并且表单正常提交。

我会通过完全删除JS并从表单控件中删除name属性来处理这个问题,而这些属性不希望出现在提交的数据中。

相关问题