Three analytic modes of sharing Ajax _ajax related

Source: Internet
Author: User

JSON format in Ajax

HTML code:

 

PHP Code:

<?php
  //receiving the request data sent by the client
  $user = $_post[' user '];
  is a JSON-formatted string
  //var_dump ($user);
  $json _user = Json_decode ($user, true);
  Var_dump ($json _user[' name '));
  $json = ' {' A ': 1, "B": 2, "C": 3, "D": 4, "E": 5} ';
  Var_dump (Json_decode ($json));
  The response data conforms to the JSON-formatted string
  //1. Manually construct
  //echo ' {' name ': ' Zhouzhiruo ', ' pwd ': ' 123456 '} ';
  2. Use the Json_encode () function
  echo json_encode ($json _user);

XML format in the second Ajax

HTML page:

 

PHP page code:


  the request data sent by the <?php//Receiving client
  $user = $_post[' user '];//a String type
  //var_dump ($user) conforming to the XML format requirements;
  Create a DOMDocument object
  $doc = new DOMDocument ();
  Call the Loadxml () method
  $result = $doc->loadxml ($user);
  Var_dump ($doc);
  How to build XML-compliant data
  /* Modify response header Content-type value is "Text/xml"
  header (' Content-type:text/xml ');
  echo $user//XML-formatted string type/
  header (' Content-type:application/xml ');
  echo $doc->savexml ();

HTML format in three Ajax

HTML page:

 

PHP Page:

<?php
  //For processing client request two-level linkage data
  //1. Receive the province information sent by the client
  $province = $_post[' provcince '];
  2. Determine current province information, provide different city information
  switch ($province) {case
    ' Shandong Province ':
      Echo ' Qingdao, Jinan, Weihai, Rizhao, Dezhou ';
      break;
    Case ' Liaoning Province ':
      Echo ' Shenyang, Dalian, Tieling, Dandong, Jinzhou ';
      break;
    Case ' Jilin Province ':
      Echo ' Changchun, Songyuan, Jilin, Tonghua, Siping ';
      break;
  The server-side response is a string
?>

The above content is small series to share Ajax three kinds of analytic mode, hope you like.

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.