PHP - AJAX and PHP

2017.05.06 08:55

졸리운_곰 조회 수:47

 

PHP - AJAX and PHP

 AJAX는 좀 더 상호적인 어플리케이션을 만들기 위해 사용됩니다.

 

 

 

 

1. AJAX PHP Example

 아래 예제는 사용자가 입력 필드에 문자를 타이핑하는 동안 웹 페이지와 웹 서버가 어떻게 통신을 하는가를 보이는 예제입니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>
<head>
<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET""gethint.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>
 
<p><b>Start typing a name in the input field below:</b></p>
<form
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
cs

 

 

 

 

 위 예제에서 사용자가 입력 필드에 문자를 입력할 때, showHint() 함수가 실행이 됩니다.

 

 

 코드 설명:

  - 첫째로, 입력 필드가 비었는지 확인합니다. 비어있다면, txtHint placeHolder의 내용을 지우고 함수를 빠져나옵니다.

 

 

 그러나, 입력 필드가 비어있지 않다면, 아래와 같은 일을 합니다:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

  - XMLHttpRequest 객체를 생성

  - 서버 반응에 대해 실행될 함수 생성

  - 서버의 PHP 파일(gethint.php)에 요청을 보냅니다.

  - q 파라미터가 gethint.php?="+str이 추가되어 알려집니다.

  - str 변수는 입력필드의 내용을 지니고 있습니다.

 

 

 

 

 

 

 

 

 

2. The PHP File - "gethint.php"

 PHP 파일은 배열의 이름을 확인하고, 상응하는 이름을 브라우저에 반환합니다:

 

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";
 
// get the q parameter from URL
$q = $_REQUEST["q"];
 
$hint = "";
 
// lookup all hints from array if $q is different from "" 
if ($q !== "") {
    $q = strtolower($q);
    $len=strlen($q);
    foreach($a as $name) {
        if (stristr($qsubstr($name0$len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}
 
// Output "no suggestion" if no hint was found or output correct values 
echo $hint === "" ? "no suggestion" : $hint;
?>
cs
 

 



출처: http://palpit.tistory.com/354 [palpit's log-b]

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
89 PHP Composer 설치 및 사용법 졸리운_곰 2018.09.27 288
88 [PHP] PhpStorm IDE 사용하기 file 졸리운_곰 2018.07.19 119
87 phpStorm 원격 서버 연결 및 배포 (Deployment) file 졸리운_곰 2018.07.18 152
86 php 세션 저장소를 redis 로 바꾸어 본 후기. file 졸리운_곰 2018.05.05 413
85 아이오닉과 php 그리고 mysql 연동 : Using PHP and MySQL with Ionic file 졸리운_곰 2018.01.14 340
84 CKEditor 4 설치와 PHP 연동 하기 file 졸리운_곰 2018.01.14 234
83 5가지 조미료같은 워드프레스 플러그인 file 졸리운_곰 2018.01.14 52
82 wordpress 한시간만에 만들기 졸리운_곰 2017.10.26 63
81 PHP 프레임 새로고침할 때 페이지 유지하기 입니다. 졸리운_곰 2017.09.17 365
80 How to install composer on Wamp file 졸리운_곰 2017.09.10 104
79 Install Composer on Windows and WAMP Server file 졸리운_곰 2017.09.10 46
78 php로 하둡 다루기 : Using Hadoop And PHP 졸리운_곰 2017.08.03 213
77 php로 빅데이터 다루기 : BIG DATA IN PHP file 졸리운_곰 2017.08.03 47
76 PHP-ML - Machine Learning library for PHP 머신러닝 file 가을의곰 2017.06.18 267
75 xampp+php+mssql file 가을의곰 2017.06.10 142
74 Build An Automated PHP Gallery System In Minutes file 가을의곰 2017.06.10 181
73 xampp+php+mssql file 가을의곰 2017.06.04 98
» PHP - AJAX and PHP file 졸리운_곰 2017.05.06 47
71 PhpStorm 으로 라라벨 개발 하기 졸리운_곰 2017.05.05 408
70 WordPress Development using PhpStorm 졸리운_곰 2017.05.05 457
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED