在URL中传递php变量
我在使用我网站的URL传递变量时遇到问题 代码如下:在URL中传递php变量,php,javascript,url,variables,Php,Javascript,Url,Variables,我在使用我网站的URL传递变量时遇到问题 代码如下: function sort(form) { var Page = "?"; var iweek = form.listWeeks.SelectedIndex; var week = form.listWeeks.options[iweek].value; var month = form.listMonth.selectedIndex+1;
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;
}
当我单击指向此功能的提交按钮时,url将更改为:
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;
}
它起作用了。。谁能告诉我可能是什么问题吗
谢谢 您可能需要在每个
标记中使用value
属性。比如说
<select name="listMonth">
<option value="1">January</option>
<option value="2">February</option>
...
</select>
一月
二月
...
您还可以将
更改为
这应该按预期工作(更新):
一月
二月
...
JavaScript代码不是必需的。在您注释掉的两行代码中有一个错误。如果看不到更多的上下文(比如形式),就不可能说这到底是为什么 这导致JavaScript崩溃,表单正常提交
我将完全废弃JS,并从表单控件中删除您不希望出现在提交的数据中的
name
属性来解决这个问题。我在每个选项标记中使用value属性。当我逐月更改列表时,它会返回到8月份,我希望它是8。如果您查看我遗漏了一周的代码块,它可以正常工作。当我在URL中添加week时,它会将URL更改为某种类型的var转储。我按照您的要求做了,现在已经修复了,感谢您的快速响应!在使用javascript进行此类操作之前,我会三思而后行!谢谢。这看起来更像是JavaScript而不是PHPcode@GeraldSchneider我还注意到,但是我认为这里不需要JavaScript
<form method="get" action="test.php">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
...
</select>
<input type="submit" />
</form>