HKGalden科技臺
發表文章發起投票
[AJAX基礎教學 - 4]Your first AJAX program
[AJAX基礎教學 - 4]Your first AJAX program – part A: Create the XML file
======================================================================================================

Welcome to the AJAX – 4 tutorial
[/size=5]

What I did in the previous tutorial?
We have just [#FF0000]created a simple user interface[/#FF0000] of the webpage, so what we have now is only a website it dosen’t do anything.
As you can see when we are typing something, it has [#FF0000]no functionality built yet[/#FF0000].

[right]Source code: http://www.sendspace.com/file/yh8xob[/right][/size=3]




What we are going to do?
To create the scripts. Now remember what I said this program consists of HTML file which is being looked as right above, a PHP file and a JavaScript file. The PHP file is stayed on the server and the JavaScript file is stayed on user computer. So that way [#FF0000]the user computer and the PHP file on the server[/#FF0000] they can [#FF0000]communicate with each other behind the scene[/#FF0000]. And [#FF0000]all the users see is just the interface[/#FF0000]; it doesn’t even see the complex itself that is going on behind the scene. But we know what really going on back there, so let’s go ahead on building them right now.

======================================================================================================

To create the XML file:

1. open notepad++ and type”[#FF0000]<?php ?>[/#FF0000]”, then save it “[#FF0000]website.php[/#FF0000]”
[#FF0000]<?php ….. ?>[/#FF0000] is the [#FF0000]basic structure of every PHP file[/#FF0000].



Why we need a PHP file?
We are learning AJAX here, but why we need a PHP file is [#FF0000]the PHP file[/#FF0000] is going to [#FF0000]generate the XML file[/#FF0000] and then to [#FF0000]send the XML file to the client computer[/#FF0000]. So it is still seen as XML file by a JavaScript file even through it is generated from the PHP code.

2. In our PHP file, we type “[#FF0000]header('Content-Type: text/xml');[/#FF0000]” inside <?php HERE ?>. This code means we are going to be [#FF0000]generating XML content[/#FF0000].

3. Under this, we type “[#FF0000]echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';[/#FF0000]”. This code is used every time when you are generating the XML file.

4. We are going to [#FF0000]make an XML element[/#FF0000]. So under this, we type “[#FF0000]echo '<response>';[/#FF0000]” and “[#FF0000]echo '</response>';[/#FF0000]”.



Now all of our codes are going to be sent and between the opening “<response>” tag and the closing </response> tag.



The highlighted area is what the JavaScript file going to see and output the result on the “result” div under the input box. For example: it is going to say “[#FF0000]We do have hkgalden ![/#FF0000]” or “[#FF0000]Sorry puck we have no hkgolden ![/#FF0000]”…

======================================================================================================

In the next tutorial, we are going to [#FF0000]finish the codes between the response tags[/#FF0000]. It is going to be created dynamically [#FF0000]using PHP[/#FF0000].
[/size=4]

Good0Bad0
2013/07/16, 1:07:04 凌晨
本貼文共有 0 個回覆
此貼文已鎖,將不接受回覆
發表文章發起投票