如何不通过<form>方式提交数据,同时又刷新整个页面

 <form method="post" action="test.php">
...
<input type="submit" />
</form>

通过表单

方式提交表单数据后,整个页面刷新,URL变成test.php。

var xhr = new XMLHttpRequest();
xhr.open("post", "test.php", true);
xht.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(...);
通过AJAX方式提交表单数据,只得到了test.php返回的数据,不会刷新整个页面,浏览器地址栏中URL不变。

那么,如果我的页面中没有表单

,但要向服务器test.php页面提交数据,并刷新整个页面,浏览器地址栏中URL变成test.php,新页面中有根据提交的数据生成的数据,该怎么做呢?

可以通过AJAX ,不知道你懂不懂,刷新页面就是另一回事了,你是php,我不知道有木有。。。

不通过form提交数据简单,你可以用jQuery的ajax去提交,任意网页中dom结构中的数据都可以提交,只要你的服务器认。
刷新页面也简单,location.reload()就可以了。

用js自己组合成键值对提交就行了,将键值对数据放到send中,刷新的话你可以提交完毕后js刷新页面即可,不过感觉没有必要。你都用ajax提交数据到test.php了干嘛还要跳转到test.php,多此一举啊
......
name:

age:

``` var s='name='+encodeURIComponent(document.getElementById('name').value); s+='age='+encodeURIComponent(document.getElementById('age').value); //...........其他的控件 var xhr = new XMLHttpRequest(); xhr.open("post", "test.php", true); xht.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.onreadystatechange=function(){ if(4==xhr.readyState){ if(200==xhr.status){alert('成功提交,服务器返回:'+xhr.responseText);location.href='test.php'} else alert('动态页错误,返回内容:'+xhr.responseText) } } xhr.send(s);//////发送键值对 ```

用js自己组合成键值对提交就行了,将键值对数据放到send中,刷新的话你可以提交完毕后js刷新页面即可,不过感觉没有必要。你都用ajax提交数据到test.php了干嘛还要跳转到test.php,多此一举啊

 name:<input type="text" id="name"/><br/>
age:<input type="text" id="age"/>
。。。。。。。其他的控件,加上id
<script>
 var s='name='+encodeURIComponent(document.getElementById('name').value);
    s+='age='+encodeURIComponent(document.getElementById('age').value);
        //...........其他的控件
        var xhr = new XMLHttpRequest();
xhr.open("post", "test.php", true);
xht.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.onreadystatechange=function(){
   if(4==xhr.readyState){
         if(200==xhr.status){alert('成功提交,服务器返回:'+xhr.responseText);location.href='test.php'}
            else alert('动态页错误,返回内容:'+xhr.responseText)
      }
}
xhr.send(s);//////发送键值对
</script>