PHP page Post method for Ajax, introductory example tutorial
1, ajax.php page, the source code is as follows:
<HTML><Head><title>Ajax Post Pass Value</title></Head><Scriptlanguage= "JavaScript">functionSaveuserinfo () {//Get accept return information layer varmsg=document.getElementById ("msg"); //get Form object and user information values varF=Document.user_info; varUserName=F.user_name.value; varUserage=F.user_age.value; varUsersex=F.user_sex.value; //receive the URL address of the form varURL= "action.php"; //The value of post is required, and each variable is joined by & varPoststr= "user_name="+UserName+"&user_age="+Userage+"&user_sex="+Usersex; //Instantiation of Ajax //var ajax = Initajax (); varAjax= false; //start initializing the XMLHttpRequest object if(window. XMLHttpRequest) {//Mozilla BrowserAjax= NewXMLHttpRequest (); if(ajax.overridemimetype) {//Set MIME categoryAjax.overridemimetype ("Text/xml"); } } Else if(window. ActiveXObject) {//IE Browser Try{Ajax= NewActiveXObject ("msxml2.xmlhttp"); } Catch(e) {Try{Ajax= NewActiveXObject ("Microsoft.XMLHTTP"); } Catch(e) {} }}if (!Ajax) { //exception, creating an object instance failedWindow.alert ("You cannot create an XMLHttpRequest object instance."); return false; } //Open connection via postAjax.open ("POST", URLs,true); //defines the transmitted file HTTP header informationAjax.setrequestheader ("Content-type","application/x-www-form-urlencoded"); //Send post Dataajax.send (POSTSTR); //Get Execution StatusAjax.onreadystatechange= function() { //If the execution status is successful, then write the return information to the specified layer if(Ajax.readystate== 4 &&Ajax.status== $) {msg.innerhtml=Ajax.responsetext; } } }</Script><Body><DivID= "MSG"></Div><formname= "User_info"Method= "POST"Action="">Name:<inputtype= "text"name= "user_name" /><BR/>Age:<inputtype= "text"name= "User_age" /><BR/>Gender:<inputtype= "text"name= "User_sex" /><BR/><inputtype= "button"value= "Submit Form"OnClick= "Saveuserinfo ()"></form></Body></HTML>
2. action.php page
<? PHP//$value =$_get[' Q '];//echo $value; $user _name=$_post[' user_name ']; $user _age=$_post[' user_age '; $user _sex= $_post[' user_sex '];echo $user _name. $user _age. $user _sex; ?>
3. The program runs as follows:
.
It is clear to see that the results of the submission appear on the current page, and that the information in the form is still there. The Ajax case that was shown here in Php,post mode is complete!
PHP page Post method for Ajax, introductory example tutorial