<script language= "JavaScript" >
<!–
function Saveuserinfo ()
{
Get the Accept return information layer
var msg = document.getElementById ("msg");
Get form objects and user information values
var f = document.user_info;
var userName = F.user_name.value;
var userage = F.user_age.value;
var usersex = F.user_sex.value;
Receive the URL address for the form
var url = "commit.php";
The value of the post is required to join each variable by &
var poststr = "Name=" + userName + "&age=" + userage + "&sex=" + usersex;
Instantiating Ajax
var ajax = null;
if (window. XMLHttpRequest) {
Ajax = new XMLHttpRequest ();
}
else if (window. ActiveXObject) {
Ajax = new ActiveXObject ("Microsoft.XMLHTTP");
}
else{
Return
}
To open a connection by post
Ajax.open ("POST", url, True);
Defines the transmitted file HTTP header information
Ajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Send post data
Ajax.send (POSTSTR);
Returns the processing function of the data
Ajax.onreadystatechange = function () {
if (ajax.readystate = = 4 && ajax.status = 200) {
msg.innerhtml = Ajax.responsetext;
}
}
}
–>
</SCRIPT>
<form name= "User_info" >
Name: <input type= "text" name= "user_name"/><br/>
Age: <input type= "text" name= "User_age"/><br/>
Sex: <input type= "text" name= "User_sex"/><br/>
<input type= "button" value= "Submitting form" onclick= "Saveuserinfo ()" >
</form>
Build a layer that accepts return information:
<div id= "MSG" ></div>
|
Note :
compared to the Get method, the Post method transmits data larger than 2K, and the application in Ajax differs in that: "The Post method's request address and the transmitted data are placed in two objects-the request address is placed in the open object, The transmitted data is placed in the Send object, and a transmission file HTTP header information (setrequestheader) is defined before the data is transmitted (commit.php), which can be $_post[' name '] , in the file with the variable $_ post[' age '], $_post[' sex ' obtains data transmitted by the Send object. and manipulate the database.