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
- <? XML version = "1.0"?>
- <Test>
- <Value> edit text </value>
- </Test>
The following code is used:
Download: Text. php
- <? PHP
- Require ("sajax. php ");
- Function showxml () {// read from test. XML, refer to PhP dom
- $ Doc = new domdocument ();
- $ Doc-> load ("test. xml ");
- $ Hello = $ doc-> getelementsbytagname ("value")-> item (0)-> nodevalue;
- Return $ hello;
- }
- Function changexml ($ text) {// write to the test. xml file
- $ Doc = new domdocument ();
- $ Doc-> load ("test. xml ");
- $ Doc-> getelementsbytagname ("value")-> item (0)-> nodevalue = $ text;
- $ Doc-> Save ("test. xml ");
- $ Hello = $ doc-> getelementsbytagname ("value")-> item (0)-> nodevalue;
- Return $ hello;
- }
- Sajax_init ();
- $ Sajax_debug_mode = 0; // disable debugging information.
- Sajax_export ("showxml", "changexml"); // sajax outputs showxml and changexml JS Functions
- Sajax_handle_client_request (); // post-Response Processing by sajax to the client
- ?>
- <HTML>
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
- <Script language = "JavaScript">
- <? PHP sajax_show_javascript ();?> // Sajax outputs Ajax code and two sajax_export Functions
- Function show (text ){
- Document. getelementbyid ("hello"). innerhtml = text;
- }
-
- Function showxml (text ){
- X_showxml (text, show); // showxml function output by sajax. "X _" is added before the original function name by default _"
- }
-
- Function changexml (text ){
- X_changexml (text, show) // The changexml function of the original PHP
-
- }
-
- VaR span, input;
-
- Function edit (){
- SPAN = Document. getelementbyid ("hello ");
- Input = Document. createelement ("input ");
- Input. setattribute ("type", "text ");
- Input. setattribute ("ID", "input ");
- Input. setattribute ("value", span. innerhtml );
- Document. Body. replaceChild (input, span );
- }
-
- Function save (){
- Changexml (input. value );
- Document. Body. replaceChild (span, input );
- }
-
- </SCRIPT>
- <Title> sajax test... </title>
- </Head>
- <Body onload = "showxml ()">
- <Span id = "hello" onclick = "Edit ()"> loading... </span>
- <P> click the text above to modify the content, so that no new page is refreshed and the content is displayed. </P>
- <Input type = "button" name = "hello" value = "modify" onclick = "Save ()">
- </Body>
- </Html>