Example of php+Ajax Method for Processing xml and json Format Data

  • 2021-11-29 06:34:55
  • OfStack

The method of processing xml and json format data by php + Ajax is described in this paper. Share it for your reference, as follows:

1. How does ajax handle the xml data format

register.php

You only need to modify the chuli function section in the previous article "php+Ajax User Name Verification without Refresh Operation"


functionchuli(){
 // window.alert("cuhli Function is called "+myXmlHttpRequest.readyState);
  // I'm going to take out the register.php Data returned 
  if(myXmlHttpRequest.readyState==4){
    //------------ See how to take out xml Data --------
    // Get mes Node 
    var mes=myXmlHttpRequest.responseXML.getElementsByTagName("mes");
    // Take out mes Node value 
    var mes_value=mes[0].childNodes[0].nodeValue;
    $("myres").value=mes_value;
  }
}

Process. php code


<?php
    // No. 1 1 The speech tells the browser that the data returned is xml Format 
    header("Content-Type:text/xml;charset=utf-8");
    // Tell the browser not to cache data 
    header("Cache-Control:no-cache");
    // Receive data ( Here, it should be related to the request mode for  _POST  Or  _GET)
    $username=$_POST['username'];
    // Here we look at how to deal with the format is xml
    $info="";
    if($username==" Li 4"){
       $info.="<res><mes> User name cannot be used , I'm sorry </mes></res>";// Note that the data here is returned to the requested page .
    }else{
       $info.="<res><mes> User name can be used with , Congratulations </mes></res>";
    }
    echo $info;
?>

2. How does ajax handle json data format

Introduction to json Format

① The format of json is as follows:

"{Attribute name: Attribute value, Attribute name: Attribute value,...}"

Because json data is the original data, so this data format is very stable, and the description ability is strong, we recommend that you use json format

② Extension of json data format

If the json returned by the server is multiple sets of data, the format should be as follows:


$info="[{" Attribute name ":" Attribute value ",...},{" Attribute name ":" Attribute value ",...},....]";

This should be done after the xmlhttprequest object receives json data


// Convert to an array of objects 
varreses=eval("("+xmlHttpRequest.responseText+")");
// Pass reses You can get whatever you want 1 Values 
reses[?]. Attribute name 

③ More complex json data format


<script language="JavaScript">
   var people ={
      "programmers":
       [
        {"firstName":"Brett", "email": "brett@newInstance.com" },
        {"firstName":"Jason", "email": "jason@servlets.com" }
       ],
      "writer":
           [
              {"writer":" Song Jiang ","age":"50"},
              {"writer":" Wu Yong ","age":"30"}
           ],
           "sex":" Male "
};
window.alert(people.programmers[0].firstName);
window.alert(people.programmers[1].email);
window.alert(people.writer[1].writer);
window.alert(people.sex);
</script>

chuli function in register. php section


function chuli(){
  if(myXmlHttpRequest.readyState==4){
    //------------ See how to take out json Data --------
    var mes= myXmlHttpRequest.responseText;
    // Use evla Function sets the mes Convert to the corresponding object 
    var mes_obj=eval("("+mes+")");
    $("myres").value=mes_obj.res;
  }
}

process. php code


<?php
  header("Content-Type: text/html;charset=utf-8");
  // Tell the browser not to cache data 
  header("Cache-Control: no-cache");
  $info="";
  if($username=="1"){
    $info='{"res":" This user is not available "}';
  }
  else{
    //$info Yes 1 A json Strings in data format 
    $info='{"res":" Congratulations, the user name is available "}';
  }
echo $info;
?>

For more readers interested in PHP related contents, please check the topics on this site: "Summary of PHP+ajax Skills and Applications", "Summary of PHP Network Programming Skills", "Introduction to PHP Basic Syntax", "Introduction to php Object-Oriented Programming", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: