PHP page Post method for Ajax, introductory example tutorial

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.