一个页面有两个<form>表单都是用get提交。
其中第一个表单的参数有$_GET[‘color’]、$_GET[‘size’]
第二个表单的参数有$_GET[‘page’]
我希望实现:
第一个表单提交后得到?color=red&size=7
再提交第二个表单,能得到?color=red&size=7&page=3,反过来亦然
现在问题就是如果提交了其中任何一个表单,再提交另一个就会重置url
比如上面的场景,提交第二个表单的时候,只能得到?page=3
额,有点被你的需求搞的晕晕的了。话说其实你可以考虑用javascript的哦,因为你是get嘛,直接跳到相应的网址去不就好了么。下面是例子:
<script type="text/javascript">
var color = document.getElementsByName('color')[0], size = document.getElementsByName('size')[0], page = document.getElementsByName('page')[0];
document.getElementById('#input1').onclick = function() {
window.location.href = '?color='+color+'&size='+size;
}
document.getElementById('input2').onclick = function() {
window.location.href = '?color='+color+'&size='+size+'&page='+page;
}
</script>
如果你想要保存input的值的话我觉得你可以考虑页面在新窗口中打开,就是说把window.location.href
换成window.open
这样,之前输入的值就还在了。当然,你还可以使用COOKIES来保存当前输入的值,这样你在一定时间内重新刷新了页面值也还是保存着的。简单的操作是这样的:
<script type="text/javascript">
//setcookies函数和getcookies函数是从w3school上手动拷贝过来的
//setcookies()用来设置cookies
function setcookies(name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays)
document.coolie = name + '=' + escape(value) + ((expiredays == null) ? '' : ';expires='+exdate.toGMTString());
}
//getcookies用来获取cookies,如果cookies不存在则返回空
function getcookies(name) {
if(document.cookie.length>0) {
start = document.cookie.indexOf(name+'=');
if(start != -1) {
start += name.length+1;
end = document.cookie.indexOf(';', start);
if(end == -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(start, end);
}
}
return "";
}
//沿用了上面的代码,没有再定义color, size, page三个变量了
document.body.onload = function() {
//input多的话可以考虑是循环~
color.value = getcookies('color');
size.value = getcookies('size');
page.value = getcookies('page');
}
function setall() {
setcookies('color', color.value, 7);
setcookies('size', size.value, 7);
setcookies('page', page.value, 7);
}
document.getElementById('input1').onclick = function() {
setall();
}
document.getElementById('input2').onclick = function() {
setall();
}
</script>
思路大概就是这样子的吧,代码是现打的,没有测试过,不保证正确性就是,不过思路应该表示的比较清楚了。
第二个表单用type=hidden的input中转参数
你可以两个表单内都有这三个元素,隐藏起来不想显示出来的。
正文完