AJAX Uses HTTP Requests

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ น่ะค่ะ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ

Moderator: phpbb, mindphp, ผู้ดูแลกระดาน

ตอบกลับโพส
ภาพประจำตัวสมาชิก
phpbb
phpBBThailand VIP Members
phpBBThailand VIP Members
โพสต์: 3189
ลงทะเบียนเมื่อ: 19 มิ.ย. 2008, 00:26
ติดต่อ:

AJAX Uses HTTP Requests

โพสต์ โดย phpbb »

AJAX Uses HTTP Requests

ในการเขียน JavaScript ถ้าต้องการส่งข้อมูลจาก ฐานข้อมูลไปยัง Server หรือส่งข้อมูลผูใช้ไปยัง Server เราต้องใช้ HTML โดยใช้ Method GET และ POST ไปยัง Server โดยเมื่อผู้ใช้กดปุ้ม Submit ก็จะมีการส่งข้อมูลไปยัง Server เพื่อตอบรับ แล้วจะมีการโหลดหน้าเว็บเพจใหม่ขึ้นมาเพราะการส่งหน้าเพจใหม่ให้ผู้ใช้ให้มี การใสค่าที่ช่องรับข้อมูล จึงทำให้มีการโหลดข้อมูลใหม่ทุกๆครั้งจึงทำให้มีความล่าช่าในการโหลดเพจแต่ ละครั้ง

ใน Ajax จะใช้ JavaScript ติดต่อโดยตรงกับ Server โดยใช้ JavaScript XMLHttpRequest objectใน HTTP request สามารถเรียกหน้าเว็บเพจและตอบกลับจาก Web server ในการรีโหลดหน้าเว็บเพจ โดยวิธีการนี้จะมีการส่งข้อมูลอยู่เบื้องหลัง ไม่ต้องโหลดข้อมูลที่มีอยู่แล้วมาอีก เพียงแต่โหลดข้อมูลใหม่มา

ตัวอย่างการใช้ Ajax

อย่างแรกที่ต้องทำคือการใช้ HTML ในการสร้างฟอร์มผู้ใช้และเวลา

<html>

<body>

<form name="myForm">

Name: <input type="text" name="username" />

Time: <input type="text" name="time" />

</form>

</body>

</html>

AJAX - Browser Support

การใช้ XMLHttpRequest object

ความแตกต่างในการใช้เบราเซอร์

ใน Internet Explorer จะใช้ ActiveXObject ในขณะที่ เบราเซอร์ที่ใช้ JavaScript จะเรียกใช้โดยXMLHttpRequest.

การสร้าง object และความแตกต่างเบราเซอร์ ต่อไปนี้จะใช้ try and catch



<html>

<body>

<script type="text/javascript">

function ajaxFunction()

{

var xmlHttp;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

return false;

}

}

}

}

</script>

<form name="myForm">

Name: <input type="text" name="username" />

Time: <input type="text" name="time" />

</form>

</body>

</html>

เมื่อใช้ try สร้าง XMLHttp=new XMLHttpRequest(). ในที่นี้จะใช้ Firefox, Opera, and Safari browsersถ้ามันฟ้องว่าผิดพลาดจะใช้ try xmlHttp=new ActiveXObject("Msxml2.XMLHTTP") โดยใช้ Internet Explorer 6.0+



AJAX - More About the XMLHttpRequest Object

หลังจากที่มีการส่งข้อมูลให้กับ Server เรามีคุฯสมบัติที่สำคัญ 3อย่าง ในการใช้ XMLHttpRequest object.

คุณสมบัติที่เรียกใช้ function นำไปประมวลผลฝั่ง Server



xmlHttp.onreadystatechange=function()

{

// We are going to write some code here

}

มาดูสถานะการทำงานการตอบรับทางฝั่ง Server



State


Description

0


The request is not initialized

1


The request has been set up

2


The request has been sent

3


The request is in process

4


The request is complete







xmlHttp.onreadystatechange=function()

{

if(xmlHttp.readyState==4)

{

// Get the data from the server's response

}

}

The responseText Property

การส่งข้อมูลกลับจาก Server ตอบรับโดย Text property.



xmlHttp.onreadystatechange=function()

{

if(xmlHttp.readyState==4)

{

document.myForm.time.value=xmlHttp.responseText;

}

}

AJAX - Request a Server

AJAX - Sending a Request to the Server การร้องขอจาก Server

เราจะใช้ method open() และ Method the send()

Method open() มี 3 อากิวเมน อากิวเมนแรก คือจะส่งการร้องขอข้อมูลจาก Server โดยใช้ GET or POST

อากิวเมนที่ 2 คือ การร้องขอโดยURL of the server-side อากิวเมนที่3 คือ การร้องขอโดย handled asynchronously

xmlHttp.open("GET","time.asp",true);

xmlHttp.send(null);

เมื่อใช้ AJAX function จะได้ดังนี้ ซึ่งจะทำงานอยู่เบื้องหลัง



<form name="myForm">

Name: <input type="text"

onkeydown="ajaxFunction();" name="username" />

Time: <input type="text" name="time" />

</form>



<html>

<body>

<script type="text/javascript">

function ajaxFunction()

{

var xmlHttp;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

return false;

}

}

}

xmlHttp.onreadystatechange=function()

{

if(xmlHttp.readyState==4)

{

document.myForm.time.value=xmlHttp.responseText;

}

}

xmlHttp.open("GET","time.asp",true);

xmlHttp.send(null);

}

</script>

<form name="myForm">

Name: <input type="text"

onkeydown="ajaxFunction();" name="username" />

Time: <input type="text" name="time" />

</form>

</body>

</html>

ตัวอย่างการใช้งาน
Type a Name in the Box Below

First Name:

Suggestions:


HTML Form

<form>

First Name:

<input type="text" id="txt1"

onkeyup="showHint(this.value)">

</form>

<p>Suggestions: <span id="txtHint"></span></p>

The showHint() Function

function showHint(str)

{

if (str.length==0)

{

document.getElementById("txtHint").innerHTML="";

return;

}

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)

{

alert ("Your browser does not support AJAX!");

return;

}

var url="gethint.asp";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}

- defind url (filename) to ส่งให้กับ Server

-ใส่ parameter (q) ให้กับ url กับที่จะใส่ข้อมูลเข้าไป

- สุ่ม random number ใหกับ Server โดยใช้ cached file

- สร้าง XMLHTTP object และ เรียก object ไปประมวลผล แล้วเปลี่ยน

- เปิดใช้ XMLHTTP object ให้กับ url

- ส่งการร้องขอ HTTP ให้กับ Server

The GetXmlHttpObject() Function



function GetXmlHttpObject()

{

var xmlHttp=null;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xmlHttp;

}

The stateChanged() Function

function stateChanged()

{

if (xmlHttp.readyState==4)

{

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

}

JavaScript Howto

เขียน JavaScript ใน HTML Page

<html>

<body>

<script type="text/javascript">

document.write("Hello World!")

</script>

</body>

</html>

จะได้ output:Hello World!

การเขียน Script ใน head section

มันสามารถทำการ executed และโหลดก่อนที่จะใช้

<html>

<head>

<script type="text/javascript">

....

</script>

</head>

การเขียน Script ในbody section

<html>

<head>

</head>

<body>

<script type="text/javascript">

....

</script>

</body>

การเขียน Script ใน head sectionและ body section

<html>

<head>

<script type="text/javascript">

....

</script>

</head>

<body>

<script type="text/javascript">

....

</script>

</body>

การประการตัวแปนใน JavaScript

Declare a Variable

การสร้างตัวแปรใน statement

var strname = some value



การสร้างตัวแปรนอก statement

strname = some value



การส่งค่าใหกับตัวแปร

var strname = "Hege"

or

strname = "Hege"





JavaScript If...Else Statements

เงื่อนไขในการใช้

- if statement ใช้เมื่อเงื่อนไขนั้นเป็นจริง

- if...else statement ใช้เมื่อเงื่อนไขนั้นเป็นจริง และเงื่อนไขอื่นๆเป็นเท็จ

- if...else if....else statement ใช้เมื่อต้องการเลือกมากกว่า 1 อย่างในการexecute

- switch statement ใช้เมื่อต้องการเลือกมากกว่า 1 อย่างในการexecute



If Statement ใช้เมื่อเงื่อนไขนั้นเป็นจริง
Syntax

if (condition)

{

code to be executed if condition is true

}

ตัวอย่าง



<html>

<body>



<script type="text/javascript">

var d = new Date()

var time = d.getHours()



if (time < 10)

{

document.write("<b>Good morning</b>")

}

</script>



<p>

This example demonstrates the If statement.

</p>



<p>

If the time on your browser is less than 10,

you will get a "Good morning" greeting.

</p>



</body>

</html>



Output

This example demonstrates the If statement.

If the time on your browser is less than 10, you will get a "Good morning" greeting.



If...else Statement ใช้เมื่อเงื่อนไขนั้นเป็นจริง และเงื่อนไขอื่นๆเป็นเท็จ
Syntax

if (condition)

{

code to be executed if condition is true

}

else

{

code to be executed if condition is not true

}







ตัวอย่าง

html>

<body>



<script type="text/javascript">

var d = new Date()

var time = d.getHours()



if (time < 10)

{

document.write("<b>Good morning</b>")

}

else

{

document.write("<b>Good day</b>")

}

</script>



<p>

This example demonstrates the If...Else statement.

</p>



<p>

If the time on your browser is less than 10,

you will get a "Good morning" greeting.

Otherwise you will get a "Good day" greeting.

</p>



</body>

</html>



Output



Good day

This example demonstrates the If...Else statement.

If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting.





if...else if....else statement ใช้เมื่อต้องการเลือกมากกว่า 1 อย่างในการexecute
Syntax

if (condition1)

{

code to be executed if condition1 is true

}

else if (condition2)

{

code to be executed if condition2 is true

}

else

{

code to be executed if condition1 and

condition2 are not true

}

ตัวอย่าง

<html>

<body>



<script type="text/javascript">

var d = new Date()

var time = d.getHours()

if (time<10)

{

document.write("<b>Good morning</b>")

}

else if (time>=10 && time<16)

{

document.write("<b>Good day</b>")

}

else

{

document.write("<b>Hello World!</b>")

}

</script>



<p>

This example demonstrates the if..else if...else statement.

</p>



</body>

</html>



Output

Good day

This example demonstrates the if..else if...else statement.

The JavaScript Switch Statementใช้เมื่อต้องการเลือกมากกว่า 1 อย่างในการexecute
Syntax

switch(n)

{

case 1:

execute code block 1

break

case 2:

execute code block 2

break

default:

code to be executed if n is

different from case 1 and 2

}

จะมี single expression n ค่าใน expression จำนำไปเปรียบเทียบกับค่าในeach case structure ถ้า match กันให้นำไป execute ใช้ break หลังจากการรันแล้ว ก็จะทำงานใน case ต่อไปอัตโนมัติ

ตัวอย่าง

<html>

<body>

<script type="text/javascript">

var d = new Date()

theDay=d.getDay()

switch (theDay)

{

case 5:

document.write("<b>Finally Friday</b>")

break

case 6:

document.write("<b>Super Saturday</b>")

break

case 0:

document.write("<b>Sleepy Sunday</b>")

break

default:

document.write("<b>I'm really looking forward to this weekend!</b>")

}

</script>



<p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>



</body>

</html>



Output

I'm really looking forward to this weekend!

This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc






Operators ในJavaScript

Arithmetic Operators

Operator


Description


Example


Result

+


Addition


x=2
y=2
x+y


4

-


Subtraction


x=5
y=2
x-y


3

*


Multiplication


x=5
y=4
x*y


20

/


Division


15/5
5/2


3
2.5

%


Modulus (division remainder)


5%2
10%8
10%2


1
2
0

++


Increment


x=5
x++


x=6

--


Decrement


x=5
x--


x=4

Assignment Operators

Operator


Example


Is The Same As

=


x=y


x=y

+=


x+=y


x=x+y

-=


x-=y


x=x-y

*=


x*=y


x=x*y

/=


x/=y


x=x/y

%=


x%=y


x=x%y

Comparison Operators

Operator


Description


Example

==


is equal to


5==8 returns false

===


is equal to (checks for both value and type)


x=5
y="5"

x==y returns true
x===y returns false

!=


is not equal


5!=8 returns true

>


is greater than


5>8 returns false

<


is less than


5<8 returns true

>=


is greater than or equal to


5>=8 returns false

<=


is less than or equal to


5<=8 returns true

Logical Operators

Operator


Description


Example

&&


and


x=6
y=3

(x < 10 && y > 1) returns true

||


or


x=6
y=3

(x==5 || y==5) returns false

!


not


x=6
y=3

!(x==y) returns true



String Operator

ตัวอย่าง

txt1="What a very"

txt2="nice day!"

txt3=txt1+txt2

Conditional Operator
Syntax

variablename=(condition)?value1:value2
Example

greeting=(visitor=="PRES")?"Dear President ":"Dear "



ถ้าตัวแปรvisitor เท่ากับPRES เมื่อใส "Dear President " ในgreeting ถ้าตัวแปรvisitor ไม่เท่ากับPRES เมื่อใส่ "Dear " ในgreeting






JavaScript Functions

ให้เบราเซอร์ executing ในการเขียน Script ให้โหลดหน้าเว็บเพจ คุณสามารถเรียกใช้ function ได้
Example

<html>

<head>



<script type="text/javascript">

function myfunction()

{

alert("HELLO")

}

</script>



</head>

<body>



<form>

<input type="button"

onclick="myfunction()"

value="Call function">

</form>



<p>By pressing the button, a function will be called. The function will alert a message.</p>



</body>

</html>















Output



วิธีการ Define a Function

syntax

function functionname(var1,var2,...,varX)

{

some code

}

var1, var2, etc ตัวแปร หรือ ค่าสามาส่งค่าไปใน function และ { and the } defines เป็นการ start และสิ้นสุดการใช้ function



การ return Statement

เป็นการส่งค่ากลับจาก function
Example

function prod(a,b)

{

x=a*b

return x

}



เมื่อเรียกใช้ function สามารถส่ง parameters ได้ดังนี้

product=prod(2,3




JavaScript For Loop

ในการเขียนโปรแกรมส่วนใหญ่ไม่สามารถหลีกเลียงการใช้ loop ได้ถ้าค้องการใช้มากกว่า 1 ครั้ง โดยสามารถใช้ได้ดังนี้



The for Loop

ใช้เมื่อต้องการมากกว่า 1 ครั้ง

Syntax

for (var=startvalue;var<=endvalue;var=var+increment)

{

code to be executed

}

Example

<html>

<body>



<script type="text/javascript">

for (i = 0; i <= 5; i++)

{

document.write("The number is " + i)

document.write("<br />")

}

</script>



<p>Explanation:</p>



<p>This for loop starts with i=0.</p>



<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>



<p><b>i</b> will increase by 1 each time the loop runs.</p>



</body>

</html>

Output

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5



JavaScript While Loop

while loop จะทำงานก็ต่อเมื่อเงื่อนไขนั้นเป็นจริง

Systax

while (var<=endvalue)

{

code to be executed

}

Example

จากข้างล่าง ให้ค้าเริ่มต้น i=0 แล้วรันไปเรื่อยๆ จนกระทั่ง i=10 จึงหลุดออกจาก loop

<html>

<body>

<script type="text/javascript">

var i=0

while (i<=10)

{

document.write("The number is " + i)

document.write("<br />")

i=i+1

}

</script>

</body>

</html>



Result

The number is 0

The number is 1

The number is 2

The number is 3

The number is 4

The number is 5

The number is 6

The number is 7

The number is 8

The number is 9

The number is 10
The do...while Loop

do..while Loop จะเป็นการทำก่อน แล้วค่อยไปเช็คเงื่อนไข ถ้าเงื่อนไขนั้นเป็นจริงก็จะทำ ถ้าเงื่อนไขนั้นเป็นเท็จก็จะไปทำงานอีกอย่างหนึ่ง

Syntax

do

{

code to be executed

}

while (var<=endvalue)

Example

<html>

<body>

<script type="text/javascript">

var i=0

do

{

document.write("The number is " + i)

document.write("<br />")

i=i+1

}

while (i<0)

</script>

</body>

</html>

Result

The number is 0
JavaScript break and continue Statements
Break

ใช้หลังจาก loop แล้ว

Example

<html>

<body>

<script type="text/javascript">

var i=0

for (i=0;i<=10;i++)

{

if (i==3){break}

document.write("The number is " + i)

document.write("<br />")

}

</script>

</body>

</html>

Result

The number is 0

The number is 1

The number is 2



Continue

เมื่อมีการ break loop ที่ใช้ขณะนั้นแล้วก็ต้องใช้ continue เพื่อส่งค่าต่อไป

Example

<html>

<body>

<script type="text/javascript">

var i=0

for (i=0;i<=10;i++)

{

if (i==3){continue}

document.write("The number is " + i)

document.write("<br />")

}

</script>

</body>

</html>

Result

The number is 0

The number is 1

The number is 2

The number is 4

The number is 5

The number is 6

The number is 7

The number is 8

The number is 9

The number is 10
JavaScript For...In Statement

for...in statement ใช้ใน loop (iterate) จะเก็บค่า elements ใน array จนกระทั่ง properties และหรือobject

Syntax

for (variable in object)

{

code to be executed

}

ตัวแปร argument สามารถมีตัวแปร array element หรือคุณสมบัติเกี่ยวกับ object
Example

<html>

<body>

<script type="text/javascript">

var x

var mycars = new Array()

mycars[0] = "Saab"

mycars[1] = "Volvo"

mycars[2] = "BMW"



for (x in mycars)

{

document.write(mycars[x] + "<br />")

}

</script>

</body>

</html>


Try...Catch Statement

การใช้ try..catch เป็นการดักข้อผิดพลาดที่เกิดจาดการเขียนโปรแกรม
Syntax

try

{

//Run some code here

}

catch(err)

{

//Handle errors here

}
Example 1

ตัวอย่างข้างล่างนี้เป็นการแสดง message "Welcome guest!" เมื่อคลิกที่ปุ่ม botton ก็จะมี

<html>

<head>

<script type="text/javascript">

function message()

{

adddlert("Welcome guest!")

}

</script>

</head>



<body>

<input type="button" value="View message" onclick="message()" />

</body>



</html>

Example 2

จะใช้ text box ในการยืนยันเมื่อคลิก ตกลง ก็จะทำงานในหน้าเว็บเพจต่อไป ถ้า method returns เป็นเท็จเมื่อผู้ใช้คลิกยกเลิก และก็จะ redirect ไปที่ผู้ใช้



<html>

<head>

<script type="text/javascript">

var txt=""

function message()

{

try

{

adddlert("Welcome guest!")

}

catch(err)

{

txt="There was an error on this page. "

txt+="Click OK to continue viewing this page, "

txt+="or Cancel to return to the home page. "

if(!confirm(txt))

{

document.location.href="http://www.w3schools.com/"

}

}

}

</script>

</head>

<body>

<input type="button" value="View message" onclick="message()" />

</body>

</html>

AJAX Database Example
Select a Name in the Box Below

Select a Customer:



CustomerID


ALFKI

CompanyName


Alfreds Futterkiste

ContactName


Maria Anders

Address


Obere Str. 57

City


Berlin

PostalCode


12209

Country


Germany





จากข้างบนเมื่อคลิกเลือก Customer แล้วจะมีการโหลดข้อมูลที่ใช้เท่านั้นส่วนอื่นๆยังคงเดิม

<html>

<head>

<script src="selectcustomer.js"></script>

</head>

<body>

<form>

Select a Customer:

<select name="customers" onchange="showCustomer(this.value)">

<option value="ALFKI">Alfreds Futterkiste

<option value="NORTS ">North/South

<option value="WOLZA">Wolski Zajazd

</select>

</form>

<p>

<div id="txtHint"><b>Customer info will be listed here.</b></div>

</p>

</body>

</html>

JavaScript code stored in the file "selectcustomer.js"

var xmlHttp



function showCustomer(str)

{

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)

{

alert ("Your browser does not support AJAX!");

return;

}

var url="getcustomer.asp";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}



function stateChanged()

{

if (xmlHttp.readyState==4)

{

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

}



function GetXmlHttpObject()

{

var xmlHttp=null;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xmlHttp;

}
AJAX XML Example
Select a CD in the Box Below

Select a CD:



TITLE: Empire Burlesque
ARTIST: Bob Dylan
COUNTRY: USA
COMPANY: Columbia
PRICE: 10.90
YEAR: 1985



<html>

<head>

<script src="selectcd.js"></script>

</head>

<body>

<form>

Select a CD:

<select name="cds" onchange="showCD(this.value)">

<option value="Bob Dylan">Bob Dylan</option>

<option value="Bonnie Tyler">Bonnie Tyler</option>

<option value="Dolly Parton">Dolly Parton</option>

</select>

</form>

<p>

<div id="txtHint"><b>CD info will be listed here.</b></div>

</p>

</body>

</html>

ต่อไปจะเป็นการเรียก "txtHint".ซึ่งใช้ค้นหาจาก web server.

เมื่อผู้ใช้เลือกข้อมูล จะเรียก function "showCD"





var xmlHttp

function showCD(str)

{

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)

{

alert ("Your browser does not support AJAX!");

return;

}

var url="getcd.asp";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}

function stateChanged()

{

if (xmlHttp.readyState==4)

{

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

}

function GetXmlHttpObject()

{

var xmlHttp=null;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xmlHttp;

}





ตัวอย่างการใช้ AJAX+PHP ติดต่อ Database Postgres

<?

// database access parameters

// alter this as per your configuration

$host = "localhost";

$user = "postgres";

$pass = "postgres";

$db = "test";

// open a connection to the database server

$connection = pg_connect ("host=$host dbname=$db user=$user

password=$pass");

if (!$connection)

{

die("Could not open connection to database server");

}

// begin a transaction block

$query = "BEGIN WORK";

$result = pg_query($connection, $query) or die("Error in query: $query.

" . pg_last_error($connection));

// generate some queries

$query = "INSERT INTO addressbook values (nextval('addressbook_id_seq'),

'Spiderman', 'The Web, Somewhere In Your Neighborhood', 'None',

'[email protected]')"; $result = pg_query($connection, $query) or

die("Error in query: $query. " . pg_last_error($connection));

$query = "INSERT INTO addressbook values (nextval('addressbook_id_seq'),

'Bruce Wayne', 'Gotham City', '64928 34585', '[email protected]')";

$result = pg_query($connection, $query) or die("Error in query: $query.

" . pg_last_error($connection));

// now roll them back

$query = "ROLLBACK";

// if you want to commit them, comment out the line above

// and uncomment the one below

// $query = "COMMIT";

$result = pg_query($connection, $query) or die("Error in query: $query.

" . pg_last_error($connection));

// now check to see how many records are there in the table

// and print this

$query = "SELECT * FROM addressbook";

$result = pg_query($connection, $query) or die("Error in query: $query.

" . pg_last_error($connection)); $rows = pg_num_rows($result); echo

"There are currently $rows records in the database";

// close database connection

pg_close($connection);

?>

ที่มา: นาย วันชัย - เรืองกาญจนไพศาล
โหลด phpBB3 ภาษาไทย ทั้งหน้าเว็บ และ admin
รวมพลคนใช้ phpBB3 แนะนำคนอื่นบ้างนะครับ ทุกคำถามจะได้มีคำตอบ แนะนำคนอื่นๆบ้างนะ ช่วยๆกันไป

สมาชิกใหม่ ก่อนถามแนะนำตัว
http://www.phpbbthailand.com/viewforum.php?f=41
กฏการใช้งานบอร์ด
http://www.phpbbthailand.com/viewtopic.php?f=37&t=5940
ตอบกลับโพส

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 219 และ บุคคลทั่วไป 0 ท่าน