Use ajax to post and get values

Source: Internet
Author: User

The following Ajax post values

<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>

<Script language = "JavaScript">
Function saveuserinfo ()
{
// Obtain the accept response information layer
VaR MSG = Document. getelementbyid ("MSG ");

// Obtain the form object and user information value
VaR F = Document. user_info;
VaR username = f. user_name.value;
VaR userage = f. user_age.value;
VaR usersex = f. user_sex.value;

// URL of the received form
VaR url = "/ajax_output.php ";

// The post value is required to connect each variable through &
VaR poststr = "user_name =" + username + "& user_age =" + userage + "& user_sex =" + usersex;

// Instantiate Ajax
// Var Ajax = initajax ();

VaR Ajax = false;
// Start initializing the XMLHTTPRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Ajax = new XMLHttpRequest ();
If (Ajax. overridemimetype) {// sets the mime category
Ajax. overridemimetype ("text/XML ");
}
}
Else if (window. activexobject) {// IE browser
Try {
Ajax = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Ajax = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Ajax) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}


// Open the connection through post
Ajax. Open ("Post", URL, true );

// Define the HTTP header information of the transmitted File
Ajax. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");

// Send post data
Ajax. Send (poststr );

// Obtain the execution status
Ajax. onreadystatechange = function (){
// If the execution status is successful, the returned information is written to the specified layer.
If (Ajax. readystate = 4 & Ajax. Status = 200 ){
MSG. innerhtml = Ajax. responsetext;
}
}
}
</SCRIPT>
<Body>
<Div id = "MSG"> </div>
<Form name = "user_info" method = "Post" Action = "">
Name: <input type = "text" name = "user_name"/> <br/>
Age: <input type = "text" name = "user_age"/> <br/>
Gender: <input type = "text" name = "user_sex"/> <br/>

<Input type = "button" value = "Submit Form" onclick = "saveuserinfo ()">
</Form>

</Body>

The following is the Ajax get value

<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>

<Script language = "JavaScript">
Function saveuserinfo ()
{
// Obtain the accept response information layer
VaR MSG = Document. getelementbyid ("MSG ");

// Obtain the form object and user information value
VaR F = Document. user_info;
VaR username = f. user_name.value;
VaR userage = f. user_age.value;
VaR usersex = f. user_sex.value;

// URL of the received form
VaR url = "/ajax_output.php? User_name = "+ username +" & user_age = "+ userage +" & user_sex = "+ usersex ";

// Instantiate Ajax
// Var Ajax = initajax ();

VaR Ajax = false;
// Start initializing the XMLHTTPRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Ajax = new XMLHttpRequest ();
If (Ajax. overridemimetype) {// sets the mime category
Ajax. overridemimetype ("text/XML ");
}
}
Else if (window. activexobject) {// IE browser
Try {
Ajax = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Ajax = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Ajax) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}


// Open the connection through post
Ajax. Open ("get", URL, true );

// Send the get data, which has been assigned a value in the URL. Therefore, you only need to add an empty parameter to send the data.
Ajax. Send (null );

// Obtain the execution status
Ajax. onreadystatechange = function (){
// If the execution status is successful, the returned information is written to the specified layer.
If (Ajax. readystate = 4 & Ajax. Status = 200 ){
MSG. innerhtml = Ajax. responsetext;
}
}
}
</SCRIPT>
<Body>
<Div id = "MSG"> </div>
<Form name = "user_info" method = "Post" Action = "">
Name: <input type = "text" name = "user_name"/> <br/>
Age: <input type = "text" name = "user_age"/> <br/>
Gender: <input type = "text" name = "user_sex"/> <br/>

<Input type = "button" value = "Submit Form" onclick = "saveuserinfo ()">
</Form>

</Body>

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.