Get and post methods for Ajax interaction with PHP

Source: Internet
Author: User

Initialize the Ajax object initajax. js

/* Initialize an XMLHTTP object */
Function initajax () {// Ajax Initialization
VaR Ajax = false;
Try {
Ajax = new actionxobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Ajax = new actionxobject ("miscrosoft. XMLHTTP ");
} Catch (e ){
Ajax = false;
}
}
If (! Ajax & typeof XMLHttpRequest! = 'Undefined '){
Ajax = new XMLHttpRequest ();
}
Return Ajax;
}

==========================================

Call page index. php

 

<! 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"> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>

<SCRIPT src = "initajax. js" type = "text/JavaScript"> </SCRIPT>

<SCRIPT type = "text/JavaScript">

// Get news (Ajax get method)
Function getnews (newsid)
{
// If the newsid parameter is not passed
If (typeof (newsid) = 'undefined ')
{
Return false;
}

VaR url = "show. php? Tag = show & id = "+ newsid; // URL address for Ajax
VaR Ajax = initajax (); // instantiate an Ajax object
Ajax. Open ("get", URL, true); // use get to request
Ajax. onreadystatechange = updatepage; // call execution status
Ajax. Send (null); // send a request to the server

// Obtain the execution status
Function updatepage ()
{
// Display the Lodding chart before the request processing ends
If (Ajax. readystate <4)
{
Show_news.innerhtml = " ";
}

// After the request processing ends, the returned content is assigned to the layer specified above.
If (Ajax. readystate = 4 & Ajax. Status = 200)
{
Show_news.innerhtml = Ajax. responsetext;
}
}

}

// Add news (Ajax POST method)
Function saveuserinfo ()
{
VaR MSG = Document. getelementbyid ("MSG"); // obtain the response information layer

// 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;

VaR url2 = "show. php? Tag = Add "; // URL of the receiving form
VaR poststr = "user_name =" + username + "& user_age =" + userage + "& user_sex =" + usersex; // the value to be post, connect each variable through &
VaR ajax2 = initajax (); // instantiate Ajax
Ajax2.open ("Post", url2, true); // open the connection through post
Ajax2.onreadystatechange = updatepage2; // call execution status
Ajax2.setrequestheader ("Content-Type", "application/X-WWW-form-urlencoded"); // defines the HTTP header information of the transmitted File
Ajax2.send (poststr); // send a request to the server

// Obtain the execution status
Function updatepage2 ()
{
If (ajax2.readystate = 4 & ajax2.status = 200)
{
MSG. innerhtml = ajax2.responsetext;
}
}
}
</SCRIPT>
</Head>

<Body onload = "saveuserinfo (); getnews (5);">
<A onclick = "getnews (5)" style = "cursor: Pointer"> News 1 </a>
<Div id = "show_news"> </div>

<Form name = "user_info">
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" onclick = "saveuserinfo ()">
</Form>
<Div id = "MSG"> </div>
</Body>
</Html>

======================================

Server execution page show. php

 

<? PHP
Include_once 'db. Class. php ';
$ Tg = new dB ('localhost', 'root', 'wangxiang ', 'fenghua', 'utf8 ');

If ($ _ Get [tag] = 'show ')
{
$ SQL = "select ID, title from news limit 0,". $ _ Get [ID];
Mysql_query ("set names 'utf8 '");
$ Item = $ Tg-> getall ($ SQL );

Sleep (1); // pause for 1 second

For ($ I = 0; $ I <count ($ item); $ I ++)
{
Echo "<a href = News. php? Nid = {$ item [$ I] ['id'] }>{$ item [$ I] ['title'] }</A> <br> ";
}
}
Else if ($ _ Get [tag] = 'add ')
{
Sleep (2); // pause for 2 seconds
If ($ _ post [user_name]! = ''& $ _ Post [user_age]! = ''& $ _ Post [user_sex]! = '')
{
$ SQL = "insert into news (title, pic_path, content) values ('". $ _ post [user_name]. "','". $ _ post [user_age]. "','". $ _ post [user_sex]. "')";
Mysql_query ("set names 'utf8 '");
$ Insert_id = $ Tg-> insert ($ SQL );

If ($ insert_id> 0)
{
Echo "inserted successfully! ";
}
}
Else
{
Echo "Incomplete Information ";
}
}
?>

 

 

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.