ajax php POST 提交例子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>利用XMLHTTP无刷新添加数据</title>

<script type="text/javascript">

<!--

function AddDatapost(sUserId,sUserName)

{

var objxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

//sUserId=escape(sUserId);

//sUserName=escape(sUserName);

var userinfo="userid="+sUserId+"&username="+sUserName;

objxmlhttp.open("Post","ajax_output.php",false);

//需要定义发送的头

objxmlhttp.setRequestHeader("Content-Length",userinfo.length)

objxmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")

objxmlhttp.send(userinfo)

//清空输入数据

document.all.userid.value="";

document.all.username.value="";

alert(objxmlhttp.responseText);

document.getElementById("xxxx").innerHTML= objxmlhttp.responseText;

}

//-->

</script>

</head>

<body>

<input type="text" name="userid"><br>

<input type="text" name="username"><br>

<input type="button" onclick="AddDatapost(document.all.userid.value,document.all.username.value)" value="AdddData">

</body>

</html>

<?

     echo $_POST['userid'];

     echo $_POST['username'];

    

?>

<script language="javascript">

var xmlHttp;

function sendCall(url, params) {

if (window.ActiveXObject) {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

} else if (window.XMLHttpRequest) {

xmlHttp = new XMLHttpRequest();

}

xmlHttp.onreadystatechange = callBack;

xmlHttp.open('POST', url, true);

xmlHttp.setrequestheader("content-length",params.length);

xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xmlHttp.setRequestHeader("Connection", "close");

xmlHttp.send(params);

}

function callBack() {

if (xmlHttp.readyState == 4) {

if (xmlHttp.status == 200) {

document.getElementById("title").innerHTML = xmlHttp.responseText ;

}

}

}

function submitMsgForm(){

var receiver = document.getElementById("receiver").value;

var title = document.getElementById("title").value;

var content = document.getElementById("content").value;

var params = "receiver="+receiver+"&title="+title+"&content="+content;

var url = "ajax_output.php";

sendCall(url, params);

}

</script>

<form name="form" action="" method="post">

<div id="xxxx"></div>

<TABLE>

<TR>

<TD>receiver</TD>

<TD><INPUT TYPE="text" NAME="receiver" id="receiver" ></TD>

</TR>

<TR>

<TD>title</TD>

<TD><INPUT TYPE="text" NAME="title" id="title" ></TD>

</TR>

<TR>

<TD>content</TD>

<TD><INPUT TYPE="text" NAME="content" id="content" ></TD>

</TR>

<TR>

<TD></TD>

<TD><INPUT TYPE="button" name="submit" value="submit" onclick="submitMsgForm()"></TD>

</TR>

</TABLE>

</form>

<?

$receiver = $_POST['receiver'];

$title = $_POST['title'];

$content =$_POST['content'];

$content = $receiver . $title . $content;

echo $content;

?>

posted on 2008-07-05 11:38  风乔  阅读(324)  评论(0编辑  收藏  举报

导航