Use ajax to modify content without refreshing new pages

Source: Internet
Author: User

Now that I am free, I have learned about Ajax. I mentioned sajax In the Ajax in action book. Let's take a look at it. It's really powerful, making it easy to write Ajax code, so I wrote a code "modifying content without refreshing the page". This feature was everywhere on the Internet. Today I finally implemented it myself. The Code contains the DOM function PhP5, this blog space only supports PhP4, so it cannot be run here.

XML file. The code is read from this file and written to this file after modification.

Download: Test. xml
  1. <? XML version = "1.0"?>
  2. <Test>
  3. <Value> edit text </value>
  4. </Test>

The following code is used:

Download: Text. php
  1. <? PHP
  2. Require ("sajax. php ");
  3. Function showxml () {// read from test. XML, refer to PhP dom
  4. $ Doc = new domdocument ();
  5. $ Doc-> load ("test. xml ");
  6. $ Hello = $ doc-> getelementsbytagname ("value")-> item (0)-> nodevalue;
  7. Return $ hello;
  8. }
  9. Function changexml ($ text) {// write to the test. xml file
  10. $ Doc = new domdocument ();
  11. $ Doc-> load ("test. xml ");
  12. $ Doc-> getelementsbytagname ("value")-> item (0)-> nodevalue = $ text;
  13. $ Doc-> Save ("test. xml ");
  14. $ Hello = $ doc-> getelementsbytagname ("value")-> item (0)-> nodevalue;
  15. Return $ hello;
  16. }
  17. Sajax_init ();
  18. $ Sajax_debug_mode = 0; // disable debugging information.
  19. Sajax_export ("showxml", "changexml"); // sajax outputs showxml and changexml JS Functions
  20. Sajax_handle_client_request (); // post-Response Processing by sajax to the client
  21. ?>
  22. <HTML>
  23. <Head>
  24. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  25. <Script language = "JavaScript">
  26. <? PHP sajax_show_javascript ();?> // Sajax outputs Ajax code and two sajax_export Functions
  27. Function show (text ){
  28. Document. getelementbyid ("hello"). innerhtml = text;
  29. }
  30.  
  31. Function showxml (text ){
  32. X_showxml (text, show); // showxml function output by sajax. "X _" is added before the original function name by default _"
  33. }
  34.  
  35. Function changexml (text ){
  36. X_changexml (text, show) // The changexml function of the original PHP
  37.  
  38. }
  39.  
  40. VaR span, input;
  41.  
  42. Function edit (){
  43. SPAN = Document. getelementbyid ("hello ");
  44. Input = Document. createelement ("input ");
  45. Input. setattribute ("type", "text ");
  46. Input. setattribute ("ID", "input ");
  47. Input. setattribute ("value", span. innerhtml );
  48. Document. Body. replaceChild (input, span );
  49. }
  50.  
  51. Function save (){
  52. Changexml (input. value );
  53. Document. Body. replaceChild (span, input );
  54. }
  55.  
  56. </SCRIPT>
  57. <Title> sajax test... </title>
  58. </Head>
  59. <Body onload = "showxml ()">
  60. <Span id = "hello" onclick = "Edit ()"> loading... </span>
  61. <P> click the text above to modify the content, so that no new page is refreshed and the content is displayed. </P>
  62. <Input type = "button" name = "hello" value = "modify" onclick = "Save ()">
  63. </Body>
  64. </Html>
Related Article

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.