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

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

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

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

What We did in the previous tutorial?

We [#FF0000]created[/#FF0000] the [#FF0000]PHP file[/#FF0000] in the pervious tutorial and [#FF0000]generated[/#FF0000] the [#FF0000]basic structure of XML[/#FF0000] using PHP code.

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

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


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

What are we going to do?
We are now [#FF0000]creating the content for the XML file[/#FF0000].
In the HTML page, user [#FF0000]type something in textbox[/#FF0000]. [#FF0000]JavaScript[/#FF0000] is going to [#FF0000]take that[/#FF0000] whatever they type in there, let’s say “hkgalden”. It is going [#FF0000]to send[/#FF0000] “hkgalden” [#FF0000]to the server[/#FF0000]. Actually, it is going to send this to the PHP codes that we are going to create them.

1. In between the “response” tags, let’s type “[#FF0000]$website = $_GET['website'];[/#FF0000][/size=3]”. The JavaScript send the texts to the server and is going to be stored in “$website” variable. You can [#FF0000]send something to PHP[/#FF0000] using one of the [#FF0000]two ways[/#FF0000]. One is “[#FF0000]GET[/#FF0000]”, another is “[#FF0000]POST[/#FF0000]”. We are using “GET” because it is a very simple program. You may ask a question where is the code to actually send a GET. It is in our JavaScript file that will be created later.

In PHP, the predefined $_GET variable is used to collect values in a form with method="get".
In PHP, the predefined $_POST variable is used to collect values in a form with method="post".[/size=3]

2. After this, we [#FF0000]create an array to store a list of websites[/#FF0000], please type in “[#FF0000]$websiteArray = array('hkgalden','HKGALDEN','galden','GALDEN','HKgalden');[/#FF0000][/size=3]”. Sometimes it may be the database, but this is a very simple program, it is okay that we are just making an array. You can pick any website you like.
So this code means we want to look at your database and see what the items you are offering to the user. But right now we use array instead. It is just for demonstration.

3. We are going to do is to [#FF0000]check[/#FF0000] if [#FF0000]the text[/#FF0000] input by the user [#FF0000]exists[/#FF0000] in our array [#FF0000]or not[/#FF0000]. So we make [#FF0000]an if-statement[/#FF0000] and use a [#FF0000]build-in function[/#FF0000] called “[#FF0000]in_array(p1,p2)[/size=3][/#FF0000]”.
Type in the following:

[#FF0000]if(in_array($website,$websiteArray))
echo 'We do have '.$website.' !';
elseif($website=='')
echo 'Enter a website you idiot';
else
echo 'Sorry punk we have no '.$website.' !';[/#FF0000][/size=3]

P1 = what variable that you want to check for
P2 = what is the name of your array[/size=3]

4. Finally, your code should be looked like this:


Code9: printout the result if the “keyword” matches in your array
Code11: printout “Enter some texts” if user didn’t type anything
Code13: printout “we have no this keyword” if the “keyword” doesn’t match in the array

Recap:
The output will be displayed in the “result” div which is under the textbox in the webpage.

[#FF0000]Alert:[/#FF0000]
[#FF0000]Do not copy these codes[/#FF0000] when you are [#FF0000]making your own website[/#FF0000], it is only for demonstration because [#FF0000]the codes are insecure[/#FF0000]. It is [#FF0000]easily hack-able[/#FF0000].

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

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