|
如果要跳转页面,那么form上要定义action跳转到相关页面,同时提交按钮的type要为submit,如下:
不跳转用ajax刷新的代码如下:
html代码:
用户注册
用户名:
密码:
JS代码:
function f1()
{
//创建xmlHttp对象
var xmlHttp;
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
//获取表单值
var username=document.form1.username.value;
var password=document.form1.password.value;
var datastr="username="+username+"&password="+password;
var url="/test2.php";
//提交数据
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
document.getElementById("d1").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(datastr);
}
PHP代码:
|
|
|