Php tutorial ajax XMLHttpRequest POST instance code
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<Html xmlns = "http://www.bkjia.com/1999/xhtml">
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Head>
<Script type = "text/javascript tutorial">
Function showUser (str ){
If (window. XMLHttpRequest) {// code for IE7 +, Firefox, Chrome, Opera, Safari
Xmlhttp = new XMLHttpRequest ();
} Else {// code for IE6, IE5
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
Document. getElementById ("txtHint"). innerHTML = xmlhttp. responseText;
}
}
Xmlhttp. open ("POST", "ajax. php", true );
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = UTF-8 ;");
Xmlhttp. send ('q = '+ str );
}
</Script>
</Head>
<Body>
<Form method = "post">
Ajax POST <input name = "submit" type = "submit" onclick = "showUser (28); return false;" value = "query"/>
</Form>
<Form action = "ajax. php" method = "post">
Real POST <input name = "q" type = "submit" value = "28"/>
</Form>
<Br/>
<Div id = "txtHint"> <B> Person info will be listed here. </B> </div>
</Body>
</Html>
Php code
<? Php
Header ('content-Type: text/html; charset = UTF-8 ;');
Echo "POST Data:". implode ('', file ('php: // input '));
Echo "<br> POST [q]". $ _ POST ['q']. "<br> ";
Print_r ($ _ POST );
?>