Вы находитесь на странице: 1из 352

PHP jQuery

PHP_title_F.indd 1

29.10.2010 19:43:57

Pro PHP and


jQuery
Jason Lengstorf

PHP_title_F.indd 2

29.10.2010 19:43:57

PHP jQuery

-
2011

PHP_title_F.indd 3

29.10.2010 19:43:57

32.973.26-018.2.75
44
681.3.07


..
. ..
. . ..
:
info@williamspublishing.com, http://www.williamspublishing.com

, .
44 PHP jQuery . : . . . : ..
, 2011. 352 . : . . . .
ISBN 978-5-8459-1693-8 (.)
32.973.26-018.2.75
.
, , , APress, Berkeley, CA.
Authorized translation from the English language edition published by APress, Copyright
2010 by Jason Lengstorf.
All rights reserved. No part of this book may be reproduced or transmitted in any form or by
any means, electronic or mechanical, including photocopying, recording or by any information
storage retrieval system, without permission from the publisher.
Russian language edition is published by Williams Publishing House according to the Agreement
with R&I Enterprises International, Copyright 2011.

PHP jQuery

. .
. .
. .
. .

25.10.2010. 70x100/16. Times.


. . . 28,38. .-. . 15,1 1500 . 0000
CtP
. . . . 197110, -, ., 15
. . , 127055, . , . , . 43, . 1
ISBN 978-5-8459-1693-8 (.)
ISBN 978-1-4302-2847-9 (.)

PHP jQuery.indb 4

, 2011
Jason Lengstorf, 2010

26.10.2010 14:12:59

I. jQuery

13

1. jQuery
2. jQuery

15
33

II. PHP

83

3. -
4.
5. ,

6.

85
113

III. jQuery PHP-

219

7.
jQuery
8. AJAX
jQuery

157
187

221
247

IV. jQuery PHP

289

9.
10. jQuery

291
321
337

PHP jQuery.indb 5

26.10.2010 14:12:59

11
12

I. jQuery

13

1. jQuery

15
15
15
16
16
16
17
17
19
19

jQuery
JavaScript
, jQuery
jQuery

Firefox
Firebug
jQuery -
jQuery
jQuery,

Google Libraries API

jQuery()
DOM- c CSS

2. jQuery
jQuery
jQuery
DOM-
DOM-
CSS



AJAX

19
19
20
21
21
32
33
33
33
34
42
54
62
63
70
76
81

II. PHP

83

3. -

85
85
86
86
86
87
88

PHP jQuery.indb 6

26.10.2010 14:12:59



Doc-



4.








-



HTML-
HTML-

5. ,




displayEvent




PHP jQuery.indb 7

7
95
99
105
107
107
111
111
112
113
113
113
114
114
116
117
118
118
120
121
122
128
135
150
156
157
157
159
161
162
165
168
171
174
175
176
179
179

26.10.2010 14:12:59

6.



Admin












III. jQuery PHP-


7.
jQuery
jQuery

jQuery
JavaScript
,
jQuery



active



AJAX


PHP jQuery.indb 8

180
183
186
187
187
188
190
190
191
200
203
203
206
207
210
210
214
217
219
221
221
222
222
223
224
226
226
226
227
229
233
238
239
246

26.10.2010 14:12:59

8. AJAX
jQuery

AJAX
AJAX

AJAX



Date



action
,




,

9
247
247
248
250
252
252
253
254
255
257
257
262
266
269
271
273
274
276
277
280
280
281
285
288

IV. jQuery PHP

289

9.

291
291
291
295
297
300
302
303
303
304
304
304

PHP jQuery.indb 9

307

26.10.2010 14:12:59

10



Calendar


JavaScript



10. jQuery
jQuery
jQuery
,

jQuery

PHP jQuery.indb 10

307
311
312
316
316
316
317
320
321
321
321
324
325
327
327
333
336
337

26.10.2010 14:12:59


-
, .

-
PHP, MySQL, AJAX -.
Ennui
Design,
-.

Humblecock,
, .

PHP jQuery.indb 11

26.10.2010 14:13:00


(Robert Banh)
,

. PHP/MySQL,
Zend CodeIgniter ,
IBM, HP, Unisys KLRU,
.

,
.

PHP jQuery.indb 12

26.10.2010 14:13:00

jQuery


jQuery . jQuery, PHP, II,
, III (
jQuery PHP).

PHP jQuery.indb 13

26.10.2010 14:13:00

PHP jQuery.indb 14

26.10.2010 14:13:00

jQuery

jQuery , , ,
JavaScript jQuery.
, JavaScript,
jQuery . jQuery, ,

.

jQuery
JavaScript -. ,
.
, ,
, ,
JavaScript -
.

JavaScript
JavaScript ,
, ,
JavaScript, JavaScript-.
JavaScript
,
, .
JavaScript XML (AJAX),
JavaScript .
JavaScript , . , JavaScript,
,

PHP jQuery.indb 15

26.10.2010 14:13:00

16

I. jQuery

,
.
. AJAX JavaScript 2.

JavaScript. ,
,
Prototype (http://www.prototypejs.org), MooTools (http://mootols.net), Yahoo! UI
Library (http://developer.yahoo.com/ui),
jQuery.

, jQuery
JavaScript .
jQuery, :





( 23 1.4);
;
;
, ;
 ;
 , http://api.jquery.com;
 , jQuery UI, .

jQuery
jQuery, , BarCamp - 2006 ( BarCamp .
http://barcamp.org). , jQuery

, , (http://ejohn.org/blog/selectors-in-javascript/).
jQuery
. ,
1.0, 26 2006 .
jQuery ( jQuery 1.4.2), ( ),
. , (plug-in), ,
.
jQuery 10.


,
, -

PHP jQuery.indb 16

26.10.2010 14:13:00

1. jQuery

17

jQuery. , , Firefox
Firebug.
,
Firefox Firebug, JavaScript.

Firefox
Firefox, http://firefox.
com Firefox ( 3.6 ). (Firefox Setup x.x.x Windows
Firefox x.x.x Mac) Firefox.

Firebug
Firebug, , Firefox, http://
getfirebug.com Install Firebug For Firefox (. 1.1).
(Install Now) .
Firefox.

. 1.1. Firebug

Firefox, , .
Firebug, (Console)
(. 1.2).

PHP jQuery.indb 17

26.10.2010 14:13:00

18

I. jQuery

. 1.2. Firebug
. Firebug JavaScript. -. Firebug
http://getfirebug.com.


,
, , , .
,
.
XAMPP
, XAMPP1, .
1. http://www.apachefriends.org/en/xamp.html XAMPP, .
2. . Windows EXE-,
. Mac DMG xampp
Applications.
3. XAMPP (XAMPP Control Panel), xampp-control.exe,
xampp, Apache.
4. , XAMPP , http://
localhost/. , XAMPP.
1
-,
- Apache, MySQL, PHP, Perl, FTP-,
phpMyAdmin . . .

PHP jQuery.indb 18

26.10.2010 14:13:00

1. jQuery

19

XAMPP Windows Mac Linux Solaris. XAMPP


. , .

jQuery -
jQuery , HTML-, . , , jQuery, , JavaScript. , jQuery ,
, .

jQuery
jQuery ,
, JavaScript.
<script type="text/javascript" src="js/jquery-1.4.2.min.js">
/script>

jQuery,

jQuery,
Google Code. , , - -,
Google Code, .
, , .
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>

Google Libraries API


Google Code jQuery Libraries API (http://code.google.com/apis/ajaxlibs). , Google, Libraries API
JavaScript .
Libraries API ,
. jQuery
- Libraries API, .
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>

PHP jQuery.indb 19

26.10.2010 14:13:00

20

I. jQuery

<script type="text/javascript">
google.load("jquery", "1.4.2");
</script>


, htdocs
XAMPP testing, index.
html. , HTML-.
<!DOCTYPE html>
<html>
<head>
<title> jQuery</title>
</head>
<body>
<p> !</p>
<p class="foo"> , .</p>
<p><span> SPAN .</span></p>
<p id="bar"> .
<span class="foo"> SPAN .</span>
</p>
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.4.2");
</script>
</body>
</html>
. , ,
, , (</body>). JavaScript
,
JavaScript.

Firefox : http://localhost/
testing (. 1.3).

. 1.3. , Firefox

PHP jQuery.indb 20

26.10.2010 14:13:00

21

1. jQuery


jQuery.

jQuery()
jQuery jQuery(). jQuery. jQuery jQuery() $(), .
, . ,
jQuery
. , , .
. $() JavaScript, .
jQuery jQuery.noConflict().
:
http://docs.jquery.com/Core/jQuery.noConflict

DOM- c CSS
, jQuery, . , (DOM) jQuery.
. DOM ,
HTML-, XHTML- XML-. , , ,
DOM ( , JavaScript) (, ), .

jQuery ,
DOM. CSS2 jQuery.
CSS HTML-.
, CSS,
. , CSS,
:
 ;
 ;
 :

-;

 .
2

http://www.w3schools.com/CSS/css_pseudo_classes.asp.

PHP jQuery.indb 21

26.10.2010 14:13:00

22

I. jQuery


,
, (ID) .
Firefox http://localhost/testing/, Firebug (Console) (. 1.4). , (Enabled).
, .
. , CSS, ,
CSS-.


(, p, div span):

(<p>) , , , 3:
$("p");

<Enter>. (. 1.4)4.
>>> $("p");
[ p, p.foo, p, p#bar ]

. 1.4. Firebug

3
(") (), <SHIFT>. . .
4
, , .
. .

PHP jQuery.indb 22

26.10.2010 14:13:00

1. jQuery

23

, . .
, (ID), foo,
bar (
). jQuery()
jQuery.


, , .
(.) :
.

foo
:
$(".foo");

.
>>> $(".foo");
[ p.foo, span.foo ]

p, span,
foo.


( id CSS) id, (#):
#id

bar :
$("#bar");

,
bar, p.
>>> $("#bar");
[ p#bar ]

, , ,
.
,
foo:
$("p.foo");

, span ,
foo.

PHP jQuery.indb 23

26.10.2010 14:13:01

24

I. jQuery

>>> $("p.foo");
[p.foo]


,
, . ,
foo , bar, :
$("p.foo,#bar");

, .
>>> $("p.foo,#bar");
[ p.foo, p#bar ]


DOM ,
. , , , . ,
,
, , ,
- .

-
, .. , ,
, c ,
(ancestor), (descendant):

span, body, Firebug :


$("body span");

span, ,
.
>>> $("body span");
[ span, span.foo ]


. ,
- (). , (parent), (>) (child):
>

PHP jQuery.indb 24

26.10.2010 14:13:01

1. jQuery

25

span, body:
$("body>span");

span, body, .
>>> $("body>span");
[ ]

span, p:
$("p>span");

.
>>> $("p>span");
[ span, span.foo ]


DOM. , ( ),
(+) , , :
+

, , :
$(".foo+p");

foo, .
>>> $(".foo+p");
[ p ]

, - :
$('p+p');

, , , ,
.
>>> $("p+p");
[ p.foo, p, p#bar

, HTML-.


, (
),
. , , ,

PHP jQuery.indb 25

26.10.2010 14:13:01

26

I. jQuery

, , ,
.. , .
, (~) :
~

,
foo, :
$(".foo~p");

.
>>> $(".foo~p");
[ p, p#bar


DOM- . ,
, , .
, .
:

, .
:()

.
. , . . jQuery.


, ,
. .
:first :last.
$("p:last");

Firebug .
>>> $("p:last");
[ p#bar ]

,
, , , :not(). , , , ,
:not() .

PHP jQuery.indb 26

26.10.2010 14:13:01

1. jQuery

27

, $("p:not(.foo)"); .
>>> $("p:not(.foo)");
[ p, p, p#bar ]


:even :odd, ,
:first :last, , , (even) (odd) 5. , $("p:odd"); .
>>> $("p:odd");
[ p.foo, p#bar ]


:eq(), . ,
$("p:eq(3)"); .
>>> $("p:eq(3)");
[ p#bar ]
. . , , 0, 1 ..


, , .

,
, ,
:contains(), .
$("p:contains()");

.
>>> $("p:contains()");
[ p.foo ]
. :contains() , .. .
, , :contains API. http://api.jquery.com/contains-selector.

, 0. . .

PHP jQuery.indb 27

26.10.2010 14:13:01

28

I. jQuery

,
, , :has(). :contains(),
, .
$("p:has(span)");

.
>>> $("p:has(span)");
[ p, p#bar


(.. , , ) :empty.
HTML-, , . , :
$(":empty");

.
>>> $(":empty");
[ script jsapi, script jquery.min.js, div#_firebugConsole ]

script div .
script jQuery, Google JSAPI,
div Firebug.


:empty :parent,
, -
() .
, , :
$("p:parent");

HTML-
(, , ),
.
>>> $("p:parent");
[ p, p.foo, p, p#bar ]


:hidden :visible
. :
$("p:visible");

HTML- , .
>>> $("p:visible");
[ p, p.foo, p, p#bar ]

PHP jQuery.indb 28

26.10.2010 14:13:01

1. jQuery

29


.
,
( , class,
href, ID title).
class.
. , ( ) , ,
(#id) (.class); ,
.



-, ([]).
[=]

.class, foo,
:
$("[class=foo]");

.
>>> $("[class=foo]");
[ p.foo, span.foo ]

,

, -, ,
, (!).
[!=]

, foo,
:
$("p[class!=foo]");

.
>>> $("p[class!=foo]");
[ p, p, p#bar ]

-
:even(),
:odd() :eq(). ,
1, 0, , , :eq().

,
, nth-child,
: even, odd, index equation.

PHP jQuery.indb 29

26.10.2010 14:13:01

30

I. jQuery

-, 1, 0, 1, 2
..
:odd ( ),
foo foo.
,
:nth-child(), :
$("p:nth-child(odd)");

.
>>> $("p:nth-child(odd)");
[ p, p ]

,

.


:first-child :last-child :first :last,
,
. , span,
p, :
>>> $("p span:last");

.
>>> $("p span:last");
[ span.foo ]

span, p,
:last-child.
$("p span:last-child");

, DOM , .
>>> $("p span:last-child");
[ span, span.foo ]


,
-, ,
.
HTML- ,
.
index.html HTML-, p script.
<form action="#" method="post">
<fieldset>
<legend> </legend>

PHP jQuery.indb 30

26.10.2010 14:13:01

1. jQuery

31

<label
<input
<label
<input

for="name"></label><br />
name="name" id="name" type="text" /><br />
for="password"></label><br />
name="password" id="password"
type="password" /><br /><br />
<label>
<input type="radio" name="loc" />

</label><br />
<label>
<input type="radio" name="loc" checked="checked" />

</label><br /><br />
<input type="submit" value="" /><br />
<label>
<input type="checkbox" name="notify"
disabled="true" />

</label><br />
</fieldset>
</form>

http://localhost/testing/
(. 1.5).

. 1.5. index.html


, , .
: :button, :checkbox, :file, :image, :input, :password, :radio,
:submit :text.

PHP jQuery.indb 31

26.10.2010 14:13:01

32

I. jQuery
, :

$("input:radio");

.
>>> $("input:radio");
[ input on, input on ]

, , .


:enabled :disabled, , . , :
$(":disabled");

.
>>> $(":disabled");
[ input on ]

:disabled.


checked,
selected. , , ,
:checked :selected.
HTML-,
:
$(":checked");

, .
>>> $(":checked");
[ input on ]

, jQuery , . ,
, XAMPP, Firefox Firebug.
DOM
jQuery. , , , jQuery,
.
DOM, jQuery.

PHP jQuery.indb 32

26.10.2010 14:13:01


jQuery

, , ,
jQuery, -.

jQuery.
,
. , .

jQuery
jQuery
, , .
,
.
$('p')
.addClass('new-class')
.text(" !")
.appendTo('body');

,
jQuery , . ,
.
, , ,
.

jQuery
jQuery . , -

PHP jQuery.indb 33

26.10.2010 14:13:01

34

I. jQuery

JavaScript
:
 DOM- CSS ( 1);
 DOM-;
 (,
);
 , , ;
 ;
 AJAX.
. jQuery. . , http://api.jquery.com.

DOM-
jQuery DOM-.
, , .
, DOM-, .
, ,
- . , , , .
. , 1. XAMPP, http://localhost/testing/ .
, Firebug ( Firebug, 1).

.eq()
,
, .eq(). : .
0.
$("p").eq(1);

Firebug .
>>> $("p").eq(1);
[ p.foo ];

PHP jQuery.indb 34

26.10.2010 14:13:01

2. jQuery

35

.eq()
(, -2
).
, , , :
$("p").eq(-3);

, .
>>> $("p").eq(-3);
[ p.foo ]

.filter() .not()
, .filter(). jQuery ,
, jQuery.
, , ,
.foo, :
$("p").filter(".foo");

.
>>> $("p").filter(".foo");
[ p.foo ]

.find() .not(), ,
. , , , foo, :
$("p").not(".foo");,

.
>>> $("p").not(".foo");
[ p, p, p#bar ]

.first() .last()
.first() .last() .eq(0) .eq(-1) .
,
:
$("p").last();,

.
>>> $("p").last();
[ p#bar ]

PHP jQuery.indb 35

26.10.2010 14:13:01

36

I. jQuery

.has()
.has() , , . , , span.
$("p").has("span");

.
>>> $("p").has("span");
[ p, p#bar ]

.is()
.is() , jQuery.
, ,
,
.
.is() , , foo.
$("p").is(".foo");

(true false), .
>>> $("p").is(".foo");
true

.slice()
,
.slice(). :
. , .
. , , . , ,
(.. 1 3), 1 4.

, .eq(),
, , .
, , :
$("p").slice(1,3);

.
>>> $("p").slice(1,3);
[ p.foo, p ]

:
$("p").slice(-2);

PHP jQuery.indb 36

26.10.2010 14:13:01

2. jQuery

37

.
>>> $("p").slice(-2);
[ p, p#bar ]

.children()
( )
.
.children(), : , .
, , , :
$("p").children();

.
>>> $("p").children();
[ span, span.foo ]

, , .children() .
, , foo,
:
$("p").children(".foo");

.
>>> $("p").children(".foo");
[ span.foo ]

.closest()
.closest() - ( ), , DOM-, (, span p, body).
, , span foo,
:
$("span.foo").closest("p");

.
>>> $("span.foo").closest("p");
[ p#bar ]

.find()
.children(), .find()
-, .
.find() .children() ,
.children() , ..

PHP jQuery.indb 37

26.10.2010 14:13:01

38

I. jQuery

, .find() -,
, .
, body, span, :
$("body").find("span");

span.
>>> $("body").find("span");
[ span, span.foo ]

.children(),
.
>>> $("body").children("span");
[ ]

.next(), .nextAll() .nextUntil()


, , : .next(), .nextAll()
.nextUntil().
.next() ,
. foo, ,
.
$("p.foo").next();

.
>>> $("p.foo").next();
[ p ]

.next() , .
$("p.foo").next("#bar");

,
, foo, bar.
>>>$("p.foo").next("#bar");
[ ]

.next() ,
.nextAll(),
, , . , foo, :
$(".foo").nextAll("p");

.
>>>$(".foo").nextAll("p");
[ p, p#bar ]

PHP jQuery.indb 38

26.10.2010 14:13:01

2. jQuery

39

. .next(), .nextAll()
.


.nextUntil(). , ,
, . , , , .
, foo
.nextUntil() "#bar".
$(".foo").nextUntil("#bar");

, bar .
>>>$(".foo").nextUntil("#bar");
[ p ]

bar, , , ,
bar, form. , .
$(".foo").nextUntil("form");

.
>>>$(".foo").nextUntil("form");
[ p, p#bar ]

.prev(), .prevAll() .prevUntil()


.prev(), .prevAll() .prevUntil()
.next(), .nextAll() .nextUntil(), , .
>>>$("#bar").prev();
[ p ]
>>>$("#bar").prevAll();
[ p, p.foo, p ]
>>>$("#bar").prevUntil(".foo");
[ p ]

.siblings()
, ,
.siblings().
, .
,
bar, .
$("#bar").siblings("p");

PHP jQuery.indb 39

26.10.2010 14:13:01

40

I. jQuery
.

>>>$("#bar").siblings("p");
[ p, p.foo, p ]

.parent()
.parent() , () . , ,
foo, :
$(".foo").parent();

.
>>>$(".foo").parent();
[ body, p#bar ]

,
foo, , , .
$(".foo").parent("p");

.
>>> $(".foo").parent("p");
[ p#bar ]

.parents() .parentsUntil()
.parents(), .parent(), .
- :
$(":checkbox").parents();

- , html.
>>>$(":checkbox").parents();
[ label, fieldset, form #, body, html ]

form,
, .
$(":checkbox").parents("form");

, -
form.
>>>$(":checkbox").parents("form");
[ form # ]

, , , (
.nextUntil() .prevUntil()), .parentsUntil().
$(":checkbox").parentsUntil("form");

PHP jQuery.indb 40

26.10.2010 14:13:01

2. jQuery

41

- ,
form.
>>>$(":checkbox").parentsUntil("form");
[ label, fieldset ]

.add()
.add() jQuery HTML.
, span foo
, .
$("p").add("span.foo");

.
>>>$("p").add("span.foo");
[ p, p.foo, p, p#bar, span.foo ]

, .add() , :
$("p").add('<span id="bat"> SPAN</span>');

.
>>>$("p").add('<span id="bat"> SPAN</span>');
[ p, p.foo, p, p#bar, span#bat ]
. , span#bat . , ,
jQuery, DOM-
. DOM-
.

.andSelf()
DOM- .
.andSelf(), .
, , span.
$("p").find("span");

span,
.
>>>$("p").find("span");
[ span, span.foo ]

span, ,
.andSelf().
$("p").find("span").andSelf();

PHP jQuery.indb 41

26.10.2010 14:13:01

42

I. jQuery

>>>$("p").find("span").andSelf();
[ p, p.foo, p, span, p#bar, span.foo ]

.contents()
.contents() .children(),
, , , ( ,
).1
span, foo,
.
$("span.foo").contents();

.
>>>$("span.foo").contents();
[ <TextNode textContent=" SPAN ."> ]

.end()
jQuery , jQuery.
.end(), jQuery , jQuery.
, span, .
>>>$("p").find("span");
[ span, span.foo ]

,
.end() .
>>>$("p").find("span").end();
[ p, p.foo, p, p#bar ]

DOM-
, ,
DOM- , ,
DOM. jQuery 1.4
.
,

Firebug.
, (. 2.1).

, , ,
(. 2.2).
1

http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1312295772.

PHP jQuery.indb 42

26.10.2010 14:13:01

2. jQuery

43

. 2.1.

. 2.2. ( )

PHP jQuery.indb 43

26.10.2010 14:13:01

44

I. jQuery

, , (Run) . <Enter>, ,
.

DOM-
DOM-, jQuery . , :
$("<p>");

, HTML-.
$('<p class="bat"> !</p>');
. HTML- , .
jQuery ;
, class (, class=\"bat\").

jQuery 1.4
JavaScript Object Notation
(JSON)2.
$("<p>", {
"class":"bat",
"text":" !"
});

.
>>>$("<p>", { "class":"bat", "text":"This is a new paragraph!" });
[ p.bat ]
. , JSON -,
, ,
, { "key":"value" } { "key1":"value1",
"key2":"value2" }.

DOM
, , , DOM. jQuery , .
, DOM ,
HTML- . , JavaScript , , ,
.
, JavaScript,
AJAX ( ), JavaScript , PHP.
2

http://ru.wikipedia.org/wiki/JSON.

PHP jQuery.indb 44

26.10.2010 14:13:02

2. jQuery

45

. , , HTML-.

.append() .prepend()
.append() .prepend() jQuery,
, , .
, .append() , .prepend() .
, .. jQuery, , (</p>).
, :
$("p").append(" jQuery.");

. ,
, .
HTML Firebug
,
(Element Inspector), Firebug.
(. 2.3).
.

. 2.3.

PHP jQuery.indb 45

26.10.2010 14:13:02

46

I. jQuery


.
, , .
HTML .
(. 2.4).

. 2.4. ,
, , , (. 2.5).
, DOM .

.append() .prepend() DOM . , , body, .


var para = $("<p>", {
"text":" !",
"css":{"background":"yellow"}
});
$("body").prepend(para);
. , , . , . .

(. 2.6).

PHP jQuery.indb 46

26.10.2010 14:13:02

2. jQuery

47

. 2.5. ,

. 2.6.

PHP jQuery.indb 47

26.10.2010 14:13:02

48

I. jQuery

.appendTo() .prependTo()
, ,
, . , jQuery .appendTo() .prependTo(), , ,
, .
, .prependTo() .
$("<p>",{
"text":" !",
"css":{"background":"yellow"}
})
.prependTo("body");

, .

.after() .before()
.after() .before() .append() .prepend(),

.
foo,
.
$("p.foo").after("<p> .</p>");

, foo (. 2.7).

. 2.7. , foo

.insertAfter() .insertBefore()
.appendTo() .prependTo()
, DOM,

PHP jQuery.indb 48

26.10.2010 14:13:02

2. jQuery

49

.insertAfter() .insertBefore() .after() .before(). .insertAfter(), , .


$("<p>", {
"text":" "
})
.insertAfter("p.foo");

(. . 2.7).

.wrap()
.wrap() , .
, .wrap(), , ,
, .
span
strong, :
$("span").wrap("<strong />");

span
(. 2.8).

. 2.8. span, strong,


, , . 2.8, "<strong />", "<strong>" "<strong>


</strong>".
, , .wrap() .
$("span").wrap("<strong><em></em></strong>");

PHP jQuery.indb 49

26.10.2010 14:13:02

50

I. jQuery

span
(. 2.9).

. 2.9. span, strong


em,


HTML, ,
. , span foo
strong, span em, .
$("span").wrap(function(){
return $(this).is(".foo") ? "<strong>" : "<em>";
});

span
, ( foo) (. 2.10).

.unwrap()
.unwrap() , .wrap(), ,
, . ,
.
span, , .
$("span").unwrap();

(
), (. 2.11).

.wrapAll()
.wrapAll()
. -

PHP jQuery.indb 50

26.10.2010 14:13:02

2. jQuery

51

, .

. 2.10.

. 2.11.
span

div , .
var div = $("<div>", {
"css":{"background-color":"yellow"}
});
$("p").wrapAll(div);

div, (. 2.12).

PHP jQuery.indb 51

26.10.2010 14:13:02

52

I. jQuery

. 2.12. div,

.wrapAll() : , DOM. , .wrapAll() strong.


$("span").wrapAll("<strong />");

,
span ,
(. 2.13).

. 2.13. span ,

PHP jQuery.indb 52

26.10.2010 14:13:02

2. jQuery

53

.wrapInner()
, .
: strong HTML, . , jQuery .wrapInner(), .
, .
$("p").wrapInner("<em />");


, (. 2.14).

. 2.14. , em p

.remove() .detach()
.remove() .detach()
DOM. DOM, .detach() jQuery, ,
,
DOM .
.remove(), .detach() , . foo , :
$("p").remove(".foo");

foo
DOM.
.remove() .detach(),
.data(),

PHP jQuery.indb 53

26.10.2010 14:13:02

54

I. jQuery

( .data() ).
DOM.
DOM .detach(), .
$("p:first").data("test"," .");
var p = $("p:first").detach();
console.log(" : "+p.data("test"));
. Firebug Firebug console .log(). ,
JavaScript , Firebug
.

.data() , DOM ,
, .data(). .
>>>$("p:first").data("test"," ...:"+p.data("test"));
: .

, .remove() .detach().
$("p:first").data("test"," .");
var p = $("p:first").remove();
console.log(" : "+p.data("test"));

, , .
>>>$("p:first").data("test"," ...:"+p.data("test"));
: null.

CSS
DOM- ,
CSS, .. , , jQuery .

.attr()
.attr().
. -, ,

. -, ,
,
.
(ID)
.
$("p:eq(3)").attr("id");

PHP jQuery.indb 54

26.10.2010 14:13:02

55

2. jQuery
.
>>>$("p:eq(3)").attr("id");
"bar"

ID "bat", :
$("#bar").attr("id", "bat");

.
>>> $("#bar").attr("id", "bat");
[ p#bat ]

bar,
.
>>> $("#bar");
[ ]

bat.
>>> $("#bat");
[ p#bat ]

, JSON
.
$("p:eq(3)").attr({
"id":"baz",
"title":" , "
});

HTML Firebug,
, .
<p id="baz" title=" , ?">
.removeAttr()

.removeAttr()
.removeAttr() ,
, .
disabled:
$(":checkbox").removeAttr("disabled");

.css()
.css() .attr(), ,
. , ; , , .
.attr(), JSON
.

PHP jQuery.indb 55

26.10.2010 14:13:02

56

I. jQuery

foo , .
$(".foo").css({
"color":"red", "background":"yellow"
});

(. 2.15).

. 2.15. CSS-
foo

, background foo :
$(".foo").css("background");

.
>>> $(".foo").css("background");
"yellow none repeat scroll 0% 0%"
. CSS3.
jQuery CSS, JavaScript.

.text() .html()
.text() .html().
, .html() HTML-, .text()
.
. .
3
http://www.456bereastreet.com/archive/200502/efficient_css_with_shorthand_
properties.

PHP jQuery.indb 56

26.10.2010 14:13:02

2. jQuery

57

bar,
:
$("#bar").text();

( ), span. .
>>> $("#bar").text();
" .
SPAN .
"

, , span, :
$("#bar").html();

.
>>> $("#bar").html();
" .
<span class="foo"> SPAN .</span>
"

, .text().
$("#bar").text(" .");

,
. ,
span: .text(), .html() .
HTML- , :
$("#bar").html(" <strong>HTML</strong>.");

,
HTML (. 2.16).

. 2.16. HTML-

PHP jQuery.indb 57

26.10.2010 14:13:03

58

I. jQuery

.val()
form .val(). , , .
submit
:
$(":submit").val();,

.
>>> $(":submit").val();
""

submit , , :
$(":submit").val("");

"".

.data()
.data() .remove() .detach(). .data() : jQuery .
, .data(), .
$("p:first")
.data("nickname", "Pookie")
.next("p")
.data("nickname", "Shnookums");
console.log(" : "+$("p:first").data("nickname"));
console.log(" : "+$("p:eq(1)").data("nickname"));

.
>>> $("p:first").data("nick...name:"+$("p:eq(1)").data("nickname"));
: Pookie
: Shnookums

JSON, .
$("p.foo").data({
"nickname":"Wubby",
"favorite":{
"movie":"Pretty Woman",
"music":"Sade", "color":"pink"
}
});
console.log(": "+$("p.foo").data("nickname"));
console.log(" :
"+$("p.foo").data("favorite").movie);

PHP jQuery.indb 58

26.10.2010 14:13:03

2. jQuery

59

>>> $("p.foo").data({"nickname":"Wubby",....data("favorite").movie);
: Wubby
: Pretty Woman

, , .
$("p.foo").data({
"nickname":"Wubby",
"favorite":{
"movie":"Pretty Woman",
"music":"Sade",
"color":"pink"
}
});
var info = $("p.foo").data(); //
//
console.log(" : "+info.nickname);
console.log(" : "+info.favorite.movie);

,
.

.addClass(), .removeClass() .toggleClass()


-,
. , .addClass()
.removeClass(), .
$("p:first").addClass("bat");
console.log(": "+$(".bat").text());
$("p:first").removeClass("bat");
console.log(": "+$(".bat").text());

, .
>>> $("p:first").addClass("bat"...le.log("Text: "+$(".bat").text());
: !
:

, .toggleClass(), ( ) ,
, , ,
.
baz
foo, :
$("p.foo").toggleClass("foo baz");

,
(. 2.17).
foo baz,
.toggleClass().
$("p.baz").toggleClass("foo baz");

, foo.

PHP jQuery.indb 59

26.10.2010 14:13:03

60

I. jQuery

. 2.17. foo baz

.hasClass()
.hasClass() .is() ,
, ,
true false. .
, foo, ,
.
var msg = $("p:eq(1)").hasClass("foo") ? "!" : "!";
console.log("? "+msg);

.height() .width()
.height() .width()
. ( 68 ,
.height() 68). .css(),
.
:
console.log(" : "+$("form").height()+"px");

.
>>> console.log(" :
"+$("form").height()+"px");
: 249px
. , .

PHP jQuery.indb 60

26.10.2010 14:13:03

2. jQuery

61

.height() .width() . 100 :


$("p").height(100).css("background","yellow");

,
(. 2.18).

. 2.18.

.innerHeight(), .innerWidth(), .outerHeight()


.outerWidth()
. .innerHeight()
.innerWidth().
, .outerHeight() .outerWidth().
, .outerHeight(true)
.outerWidth(true).
foo , .
var el = $("p.foo");
el.css({
"margin":"20px",
"border":"2px solid black"
});
console.log(" : "+el.innerWidth()+"px");
console.log(" : "+el.innerHeight()+"px");
console.log(" : "+el.outerWidth()+"px");
console.log(" : "+el.outerHeight()+"px");
console.log(" : "+el.outerWidth(true)+"px");
console.log(" : "+el.outerHeight(true)+"px");

PHP jQuery.indb 61

26.10.2010 14:13:03

62

I. jQuery
.

>>> var el = $("p.foo"); el.c...rgins: "+el.outerHeight(true)+"px");


: 598px
: 20px
: 602px
: 24px
: 642px
: 64px
. -, .


,
.

.map() .each()
.map() .each()
,
. : DOM-.
,
.map() , , , .each() , . ,
.each() , .map() .
foo
, .
$("p,.foo").map(function(index, ele){
$(this).append(" "+ele.tagName+" #"+index);
});

(. 2.19).
.each(), .map().
$("p,.foo").each(function(index, ele){
$(this).append(" "+ele.tagName+" #"+index);
});

.
, .map()
.each(). ,
foo, , span foo ,
, .
$("p,.foo").map(function(index, ele){
$(this).append(" "+ele.tagName+" #"+index);
})

PHP jQuery.indb 62

26.10.2010 14:13:03

63

2. jQuery
.find("span.foo")
.css({
"color":"red",
"background":"yellow"
});

. 2.19. map()

, span .
, , .map(),
.
, , .map() .each().
$("p,.foo").each(function(index, ele){
$(this).append(" "+ele.tagName+" #"+index);
})
.find("span.foo")
.css({
"color":"red",
"background":"yellow"
});

(. 2.20).


jQuery
, ,
, JavaScript, jQuery. JavaScript
, .

PHP jQuery.indb 63

26.10.2010 14:13:03

64

I. jQuery

. 2.20. each()
. , . , , jQuery API (http://api.
jquery.com).

.show() .hide()

.show() .hide(). display:none; .
bar, :
$("#bar").hide();

, - DOM
. ,
.show():
$("#bar").show();

.
, , ( ), , .
bar, 2 ,
.
$("#bar")
.css({
"background":"yellow",
"border":"1px solid black"

PHP jQuery.indb 64

26.10.2010 14:13:03

2. jQuery

65

})
.hide(2000,function(){
console.log(" !");
});

CSS
.hide().
, .
, : " !".
. .

.fadeIn(), .fadeOut() .fadeTo()


( )
.fadeIn() .fadeOut(). 0 1 (fadeIn()), 1 0 (fadeOut). display:none;.
display:none; , .
, ( 400 ),
, . , "fast" "slow", 200
600 .
, ,
, .
$("form")
.fadeOut(1000, function(){
console.log(" !");
})
.fadeIn(1000, function(){
console.log(" !");
});

, .fadeTo(),
, .
: ( 0 1). , .
50 % , .
$("form")
.fadeTo(1000, 0.5, function(){
console.log(" 50%!");
});

.slideUp(), .slideDown() .slideToggle()


.slideUp() 0. ,

PHP jQuery.indb 65

26.10.2010 14:13:03

66

I. jQuery

display:none, .
.slideDown() : display:none
0 .
.fadeIn() .fadeOut(),
: .
foo , ,
.
$("p.foo")
.slideUp(1000, function(){
console.log("!");
})
.slideDown(1000, function(){
console.log("!");
});

.slideToggle() , .slideUp() .slideDown(),


,
, , .
, ,
foo.
$("p.foo")
.slideToggle("slow", function(){
console.log("Toggled!");
});

, , , .

.animate()
, , .animate() .
.animate() CSS-
(easing), . "linear" "swing",
jQuery
( ).
.animate() , . CSS- JSON ,
, : ( ), , ,
. CSS JSON JSON .
animane(),
bar,
5 "swing" .

PHP jQuery.indb 66

26.10.2010 14:13:03

2. jQuery

67

$("#bar")
.css({
"background":"yellow",
"border":"1px solid black"
})
.animate({
"width":"500px",
"height":"100px"
},
5000,
"swing",
function(){
console.log(" !");
});

,
(. 2.21).

. 2.21.

animate()
.
$("#bar")
.css({
"background":"yellow",
"border":"1px solid black"
})
.animate({
"width":"500px",
"height":"100px"

PHP jQuery.indb 67

26.10.2010 14:13:03

68

I. jQuery
},
{
"duration":5000,
"easing":"swing",
"complete":function(){
console.log(" !");
}
});

. animate() . , ,
.
$("#bar")
.css({
"background":"yellow",
"border":"1px solid black"
})
.animate({
"width":"500px",
"height":"100px"
},
{
"duration":5000,
"easing":"swing",
"complete":function(){
console.log(" !");
},
"step":function(){
console.log(" !");
},
"queue":true,
"specialEasing":{
"width":"linear"
}
});

step , . , "
!".
queue , ,
.. . ,
, , , ..
specialEasing
CSS- .
. specialEasing, (James Padolsey),
jQuery 1.4. :
http://james.padolsey.com/demos/jquery/easing/easing-jq14.html

PHP jQuery.indb 68

26.10.2010 14:13:03

2. jQuery

69

.delay()
.delay(), jQuery 1.4, , .
,
.
bar , 3 ,
, .
$("#bar")
.css({
"background":"yellow",
"border":"1px solid black"
})
.slideUp(1000, function(){
console.log(" !");
})
.delay(3000)
.slideDown(1000, function(){
console.log(" !");
});

.stop()
.stop().
: , , .
false.
, ,
200 ,
.
var count = 0; //
$("#bar")
.css({
"background":"yellow",
"border":"1px solid black"
})
.animate({
"width":"500px"
},
{
"duration":6000,
"step":function(){
if(count++==200)
{
$(this).stop(true, true);
}
}
});

PHP jQuery.indb 69

26.10.2010 14:13:03

70

I. jQuery


. jQuery ,
.


, .

.error()
. , , , .
.error() (.. , ).
, ,
,
.
$("<img />", {
"src":"not/an/image.png",
"alt":" "
})
.error(function(){
console.log(" !");
})
.appendTo("body");

.
>>> $("<img />", { "src":"not/an/image.png", ...!");})
.appendTo("body");
[ img image.png ]
!

.scroll()
scroll. .scroll().
$(window)
.scroll(function(){
console.log(" !");
});

,
.
.scroll() scroll. , ,
:
$(window).scroll();

PHP jQuery.indb 70

26.10.2010 14:13:03

2. jQuery

71


JavaScript ,
- . , ,
, , ,
.

.ready()
jQuery , ,
.ready(). , DOM- .

, .ready().
$(document).ready(function(){
// jQuery });

, .ready() , jQuery.
jQuery, , $ jQuery.noConflict() (
JavaScript, $). $, .
jQuery(document).ready(function($){
// jQuery
$("p").fadeOut();
});

.
jQuery(document).ready(function(xTest){
xTest("#bar").click(function(){console.log("!");});
});

, .
, ,
, .ready().
, jQuery
.ready().
jQuery(function($){
// , DOM
});

.unload()
, ,
, (Forward) (Back)
, unload.

PHP jQuery.indb 71

26.10.2010 14:13:03

72

I. jQuery

unload -. ,
, . Google
unload .
$("<a>", {
"href":"http://google.com",
"text":" Google!"
})
.appendTo("#bar");
$(window).unload(function(){
alert("! ''! ");
});

. , Google.


, , jQuery
, .
blur, focus, focusin, focusout, load, resize, scroll, unload,
click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter,
mouseleave, change, select, submit, keydown, keypress, keyup error.

.bind() .unbind()
.bind().
.
, , . ,
.bind()
JSON.
click, .
$("p")
.bind("click", function(){
console.log(" !");
});

. click, mouseover, .
$("p")
.bind("click mouseover",function(){
console.log(" !");
});

, .
, .
JSON, , .

PHP jQuery.indb 72

26.10.2010 14:13:03

2. jQuery

73

, .
,
,
.
// notice
var notice = " !";
$("p.foo").bind("click", { n:notice }, function(event){
console.log(event.data.n);
});
// "notice"
var notice = " !";
$("#bar").bind("click", { n:notice }, function(event){
console.log(event.data.n);
});

click mouseover , .
$("p")
.bind({
"click":function(){
console.log("!");
},
"mouseover":function(){
console.log(" !");
}
});


.
.unbind(). , . , ,
. , , .
, :
$("p").unbind();

:
$("p").unbind("click");

- ,
.
var func1 = function(){
console.log(" !");
},
func2 = function(){
console.log(" !");
};
$("#bar")
.bind("click", func1)

PHP jQuery.indb 73

26.10.2010 14:13:04

74

I. jQuery
.bind("click", func2)
.trigger("click") //
.unbind("click", func1);

(
func1 func2), click bar,
( .trigger() )
, func1.
, func2.

.live() .die()
.bind() .unbind(), .live() .die()
. , .live() JavaScript
, , DOM, .
, click a
.
$("a")
.live("click", function(){
console.log(" !");
return false; //
});

, - . , a
bar .
$("<a>", {
"href":"http://google.com",
"text":" Google!"
})
.appendTo("#bar");

, , DOM a,
, .
.bind()
. , click,
.live(), .unbind(); .die(). .die() ,
.unbind().

.one()
.one() .bind() , , one() ,
. bar
, ,
.
$("#bar").one("click", function(){
console.log(" .");
});

PHP jQuery.indb 74

26.10.2010 14:13:04

2. jQuery

75

bar , .

.toggle()
.toggle() click , .
, .show() .hide(),
, .slideToggle() .slideUp() .slideDown().
, bar
, .
$("#bar")
.toggle(function(){
console.log(" 1");
},
function(){
console.log(" 2");
},
function(){
console.log(" 3");
});

bar
. bar :
$("#bar").toggle();

.
.
,
.
$("#bar").toggle(2000);

, ,
: .
$("#bar").toggle(true); //
$("#bar").toggle(false); //

.trigger()
.trigger(). , , , .
bar , .
$("#bar")
.bind("click", function(){
console.log("!");
})
.trigger("click");

PHP jQuery.indb 75

26.10.2010 14:13:04

76

I. jQuery
, , .

//
var note = " !";
$("#bar")
.bind("click", function(event, msg){ // 2-
//
// "msg" ,
// ,
var log = msg || "!";
console.log(log);
})
.trigger("click", [ note ]); //
//

, note.


(shortcut),
. , .trigger() .
.blur(), .focus(), .focusin(),
.focusout(), .load(), .resize(), .scroll(), .unload(), .click(), .dblclick(),
.mousedown(), .mouseup(), .mousemove(), .mouseover(), .mouseout(), .mouseenter(),
.mouseleave(), .change(), .select(), .submit(), .keydown(), .keypress(), .keyup()
.error().
,
click .
$("#bar").click(function(){ console.log("!"); }).click();

AJAX
jQuery, ,
, , , , , jQuery. ,
AJAX4,
, , AJAX JavaScript.
. AJAX, :
http://ru.wikipedia.org/wiki/Ajax

,
AJAX.
testing ajax.php. .
<?php
echo '<p class="ajax"> AJAX.</p>',
'<pre>GET variables: ', print_r($_GET, TRUE), '</pre>',
'<pre>POST variables: ', print_r($_POST, TRUE), '</pre>';
?>

http://en.wikipedia.org/wiki/Ajax_(programming).

PHP jQuery.indb 76

26.10.2010 14:13:04

2. jQuery

77

, AJAX, jQuery. .

$.ajax()
AJAX
$.ajax(). , , jQuery. AJAX , DOM.
$.ajax() ,
AJAX. , ,
.
$.ajax() , .
http://api.jquery.com/jQuery.ajax.
.
 data. , , (key1=val1&key2=val2),
JSON ({"key1":"val1","key2":"val2"}).
 dataFilter(data, type). ,
, .
 dataType. , . jQuery
. "xml", "html", "script", "json",
"jsonp" "text".
 error(XMLHttpRequest, textStatus, errorThrown). , .
XMLHttpRequest, .
 success(data, textStatus, XMLHttpRequest). , . , ,
XMLHttpRequest.
 type. . GET, POST. PUT DELETE,
.
 url. URL, .
POST- bar .
$.ajax({
"type":"POST",
"url":"ajax.php",
"data":"var1=val1&var2=val2",
"success":function(data){
$("#bar")
.css("background","yellow")

PHP jQuery.indb 77

26.10.2010 14:13:04

78

I. jQuery
.html(data);
}
});

(. 2.22).

. 2.22. AJAX, ajax.php

$.ajaxSetup()
AJAX $.ajaxSetup(). , ,
AJAX ajax.php POST,
bar, .
$.ajaxSetup({
"type":"POST",
"url":"ajax.php",
"success":function(data){
$("#bar")
.css("background","yellow")
.html(data);
}
});

AJAX,
.
$.ajax({
"data":{
"newvar1":"value1",

PHP jQuery.indb 78

26.10.2010 14:13:04

2. jQuery

79

"newvar2":"value2"
}
});

ajax.
php (. 2.23).

. 2.23. AJAX


$.ajax() .
$.ajax({
"type":"GET",
"data":{
"newvar1":"value3",
"newvar2":"value4"
}
});

GET (. 2.24).

AJAX
,
AJAX. ,
, $.ajax() .
, ,
$.ajax(). , , .

PHP jQuery.indb 79

26.10.2010 14:13:04

80

I. jQuery

. 2.24. , ,
GET

$.get() $.post()
GET POST $.get()
$.post(). : URL,
; ,
; , ; dataType.
ajax.php GET
, .
$.get("ajax.php", function(data){
$("#bar")
.css("background","yellow")
.html(data);
});

POST .
$.post("ajax.php", {"var1":"value"}, function(data){
$("#bar")
.css("background","yellow")
.html(data);
});

$.getJSON()
$.getJSON() JSON. URL, ,
.

PHP jQuery.indb 80

26.10.2010 14:13:04

2. jQuery

81

.
testing json.php
JSON:
{"var1":"value1","var2":"value2"}

json.php bar.
$.getJSON("json.php",
function(data){
$("#bar")
.css("background","yellow")
.html(data.var1+","+data.var2);
});

"value1, value2".

$.getScript()
JavaScript $.get
Script(). URL, ,
( , ).
testing script.php
:
alert(" AJAX!");

, :
$.getScript("script.php");

.load()
.load() $.get() $.post(),
, , .
, HTML- , , , .
: URL, ( ). ajax.php
bar , :
$("#bar").load("ajax.php", {"var1":"value1"});

. jQuery API,
, . ,

PHP jQuery.indb 81

26.10.2010 14:13:04

82

I. jQuery

URL- API. , .slideup(), : http://api.


jquery.com/slideup.
PHP,
- , , 4.

PHP jQuery.indb 82

26.10.2010 14:13:04

II


PHP

jQuery
, PHP . ,
- . , ,
jQuery. , PHP
(, , ) ,
PHP, PHP
24 3- (, 2008 .).

PHP jQuery.indb 83

26.10.2010 14:13:04

PHP jQuery.indb 84

26.10.2010 14:13:04

- () , .
, , ,
.


, -
,
.
DRY (. Dont Repeat Yourself ).
. DRY-
:
http://en.wikipedia.org/wiki/Don't_repeat_yourself.

DRY- , , , . , , ,
,
.
- ,
,
, , . , , , .

PHP jQuery.indb 85

26.10.2010 14:13:04

86

II. PHP


,
.
, .


, : ,
, , .
.
, . , , .
, . , , ,
, :
, . ,
,
.
, , .
.
,
: , 150 , ,
.


, , .
: class, , , ({}),
, .
<?php
class MyClass
{
//
}
?>

,
, new:
$obj = new MyClass;

var_
dump():
var_dump($obj);

PHP jQuery.indb 86

26.10.2010 14:13:04

3. -

87

,
test.php, testing.
<?php
class MyClass
{
//
}
$obj = new MyClass;
var_dump($obj);
?>

http://localhost/testing/test.php,
.
object(MyClass)#1 (0) { }


,
, .
, , , ,
.
MyClass, ,
.
<?php
class MyClass
{
public $prop1 = " !";
}
$obj = new MyClass;
var_dump($obj);
?>

public , . , (
).
, .
echo $obj->prop1;

, ,
, .
, , , (->).

PHP jQuery.indb 87

26.10.2010 14:13:04

88

II. PHP

, test.php, ,
,
.
<?php
class MyClass
{
public $prop1 = " !";
}
$obj = new MyClass;
echo $obj->prop1
?>

, .
!


, . , , .
, ,
$prop1, ,
.
<?php
class MyClass
{
public $prop1 = " !";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
$obj = new MyClass;
echo $obj->prop1
?>
. $this. , $this , .

, , , , .

PHP jQuery.indb 88

26.10.2010 14:13:04

3. -

89

$prop1 MyClass,
, , .
<?php
class MyClass
{
public $prop1 = " !";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
$obj = new MyClass;
//
echo $obj->getProperty();
//
$obj->setProperty(" !");
//
echo $obj->getProperty();
?>

, .
!
!

, . MyClass
.
<?php
class MyClass
{
public $prop1 = " !";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
//

PHP jQuery.indb 89

26.10.2010 14:13:04

90

II. PHP

$obj = new MyClass;


$obj2 = new MyClass;
// $prop1
echo $obj->getProperty();
echo $obj2->getProperty();
//
$obj->setProperty(" !");
$obj->setProperty(" !");
// $prop1
echo $obj->getProperty();
echo $obj2->getProperty();
?>

, .

!
!
!
!

, ,
, .
, PHP
, .. , .
.



. PHP
__construct, ,
.
, MyClass
, , .
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}

PHP jQuery.indb 90

26.10.2010 14:13:04

3. -

91

public function getProperty()


{
return $this->prop1 . "<br />";
}
}
//
$obj = new MyClass;
// $prop1
echo $obj->getProperty();
//
echo " .<br />";
?>
. __CLASS__ , , . , PHP, :
http://us3.php.net/manual/en/language.constants.predefined.php

.
"MyClass"!
!
!


__destruct(), .
, (,
).
,
MyClass __destruct().
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()

PHP jQuery.indb 91

26.10.2010 14:13:04

92

II. PHP
{
return $this->prop1 . "<br />";
}

}
//
$obj = new MyClass;
// $prop1
echo $obj->getProperty();
//
echo " .<br />";
?>

, ,
.
"MyClass"!
!
!
"MyClass"!

, PHP ,
, .
MyClass.
,
unset().
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}

PHP jQuery.indb 92

26.10.2010 14:13:04

3. -

93

}
//
$obj = new MyClass;
// $prop1
echo $obj->getProperty();
//
unset($obj);
//
echo " .<br />";
?>

, , .
"MyClass"!
!
"MyClass"!
!


:
__toString().
, __toString(),
. , , echo .
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()

PHP jQuery.indb 93

26.10.2010 14:13:04

94

II. PHP
{
return $this->prop1 . "<br />";
}

}
//
$obj = new MyClass;
//
echo $obj;
//
unset($obj);
//
echo " .<br />";
?>

.
"MyClass"!
Catchable fatal error: Object of class MyClass could not
be converted to string in
C:\XAMPP\xampp\htdocs\testing\test.php on line 33

, __toString().
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}

PHP jQuery.indb 94

26.10.2010 14:13:05

3. -

95

public function getProperty()


{
return $this->prop1 . "<br />";
}
}
//
$obj = new MyClass;
//
echo $obj;
//
unset($obj);
//
echo " .<br />";
?>


getProperty(). , .
"MyClass"!
toString: !
"MyClass"!
!
. , , . PHP, :
http://us2.php.net/manual/en/language.oop5.magic.php


extends. , MyClass
, .
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}

PHP jQuery.indb 95

26.10.2010 14:13:05

96

II. PHP
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}

}
class MyOtherClass extends MyClass
{
public function newMethod()
{
echo ' "', __CLASS__, '".<br />';
}
}
//
$newobj = new MyOtherClass;
//
echo $newobj->newMethod();
//
echo $newobj->getProperty();
?>

, .
"MyClass"!
"MyOtherClass".
!
"MyClass"!


,
.
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()

PHP jQuery.indb 96

26.10.2010 14:13:05

3. -

97

{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
echo ' "', __CLASS__, '".<br />';
}
public function newMethod()
{
echo ' "', __CLASS__, '".<br />';
}
}
//
$newobj = new MyOtherClass;
//
echo $newobj->newMethod();
//
echo $newobj->getProperty();
?>

.
"MyOtherClass".
"MyOtherClass".
!
"MyClass"!

PHP jQuery.indb 97

26.10.2010 14:13:05

98

II. PHP



,
, parent
(::).
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
//
parent::__construct();
echo ' "', __CLASS__, '".<br />';
}
public function newMethod()
{
echo ' "', __CLASS__, '".<br />';
}
}

PHP jQuery.indb 98

26.10.2010 14:13:05

3. -

99

//
$newobj = new MyOtherClass;
//
echo $newobj->newMethod();
//
echo $newobj->getProperty();
?>

,
.
"MyClass"!
"MyOtherClass".
"MyOtherClass".
!
"MyClass"!


, , . , , .
, : (public), (protected)
(private). , (static),
.
. PHP 5.
PHP 4 PHP :
http://us2.php.net/manual/en/language.oop5.php


, , (public). , , , .


(protected), ,
-
(, , ).
getProperty() MyClass
.
<?php
class MyClass
{
public $prop1 = " !";

PHP jQuery.indb 99

26.10.2010 14:13:05

100

II. PHP

public function __construct()


{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
protected function getProperty()
{
return $this->prop1 . "<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
parent::__construct();
echo ' "', __CLASS__, '".<br />';
}
public function newMethod()
{
echo ' "', __CLASS__, '".<br />';
}
}
//
$newobj = new MyOtherClass;
//
echo $newobj->getProperty();
?>

.
"MyClass"!
"MyOtherClass".
Fatal error: Call to protected method

PHP jQuery.indb 100

26.10.2010 14:13:05

3. -

101

MyClass::getProperty() from context '' in


C:\XAMPP\xampp\htdocs\testing\test.php on line 53

MyOtherClass getProperty().
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
protected function getProperty()
{
return $this->prop1 . "<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
//
parent::__construct();
echo ' "', __CLASS__, '".<br />';
}
public function newMethod()
{
echo ' "', __CLASS__, '".<br />';
}
public function callProtected()

PHP jQuery.indb 101

26.10.2010 14:13:05

102

II. PHP

{
return $this->getProperty();
}
}
//
$newobj = new MyOtherClass;
//
echo $newobj->callPropected();
?>

.
"MyClass"!
"MyOtherClass".
!
"MyClass"!


, (private), , . ,
, , .
, getProperty() MyClass
callProtected() MyOtherClass.
<?php
class MyClass
{
public $prop1 = " !";
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}

PHP jQuery.indb 102

26.10.2010 14:13:05

3. -

103

private function getProperty()


{
return $this->prop1 . "<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
//
parent::__construct();
echo ' "', __CLASS__, '".<br />';
}
public function newMethod()
{
echo ' "', __CLASS__, '".<br />';
}
public function callProtected()
{
return $this->getProperty();
}
}
//
$newobj = new MyOtherClass;
//
echo $newobj->callProtected();
?>

.
"MyClass"!
"MyOtherClass".
Fatal error: Call to private method
MyClass::getProperty() from context 'MyOtherClass' in
C:\XAMPP\xampp\htdocs\testing\test.php on line 50


, (static), ;
,
.

,
. , ,
, - .

PHP jQuery.indb 103

26.10.2010 14:13:05

104

II. PHP

, MyClass
$count plusOne(). do...while $count, 10.
<?php
class MyClass
{
public $prop1 = " !";
public static $count = 0;
public function __construct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __destruct()
{
echo ' "', __CLASS__, '"!<br />';
}
public function __toString()
{
echo " toString: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
private function getProperty()
{
return $this->prop1 . "<br />";
}
public static function plusOne()
{
return " : " . ++self::$count . ".<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
//
parent::__construct();
echo ' "', __CLASS__, '".<br />';
}

PHP jQuery.indb 104

26.10.2010 14:13:05

3. -

105

public function newMethod()


{
echo ' "', __CLASS__, '".<br />';
}
public function callProtected()
{
return $this->getProperty();
}
}
do
{
// plusOne MyClass
echo MyClass::plusOne();
} while (MyClass::$count < 10);
?>
. ($) .

, .

:
:
:
:
:
:
:
:
:
:

1
2
3
4
5
6
7
8
9
10

Doc-
Doc- (DocBlock) ,
. ,
, SDK (Software Development Kit ),
Eclipse (http://eclipse.org/) NetBeans (http://netbeans.org/), .
Doc- ,
.
/**
* Doc-
*/

Doc- , @, .
, ,
.

PHP jQuery.indb 105

26.10.2010 14:13:05

106

II. PHP

.
 @author. (
, , ).
Doc- @author,
. : John Doe <john.doe@email.com>.
 @copyright. . : 2010 __
.
 @license. . : http://www.example.com/path/to/license.txt _.
 @var. . : _.
 @param. . : $_ _.
 @return. ,
. : __.
Doc.
<?php
/**
*
*
* ,
* . ,
* .
*
* ,
* , .
*
* @author Jason Lengstorf <jason.lengstorf@ennuidesign.com>
* @copyright 2010 Ennui Design
* @license http://www.php.net/license/3_01.txt PHP License 3.01
*/
class SimpleClass
{
/**
*
*
*
* @var string:
*/
public $foo;
/**
* $foo
*
*
* @param string $val:
* @return void

PHP jQuery.indb 106

26.10.2010 14:13:05

3. -

107

*/
public function __construct($val)
{
$this->foo = $val;
}
/**
*
*
*
*
*
* @param int $bat:
* @param int $baz:
* @return int:
*/
public function bar($bat, $baz)
{
return $bat * $baz;
}
}
?>

,
Doc- : , , , ,
, .
. Doc- :
http://ru.wikipedia.org/wiki/PHPDoc

- . - , .


,
. ,
.
,
, . , , , . ,
.

PHP jQuery.indb 107

26.10.2010 14:13:05

108

II. PHP


, .
<?php
function changeJob($person, $newjob)
{
//
$person['job'] = $newjob;
return $person;
}
function happyBirthday($person)
{
// 1
++$person['age'];
return $person;
}
$person1 = array(
'name' => 'Tom',
'job' => 'Button-Pusher',
'age' => 34
);
$person2 = array(
'name' => 'John',
'job' => 'Lever-Puller',
'age' => 41
);
//
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
// ,
$person1 = changeJob($person1, 'Box-Mover');
$person1 = happyBirthday($person1);
//
$person2 = happyBirthday($person2);
//
echo "<pre> Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre> Person 2: ", print_r($person2, TRUE), "</pre>";
?>

, .
Person 1: Array
(
[name] => Tom
[job] => Button-Pusher
[age] => 34
)

PHP jQuery.indb 108

26.10.2010 14:13:05

3. -

109

Person 2: Array
(
[name] => John
[job] => Lever-Puller
[age] => 41
)
Person 1: Array
(
[name] => Tom
[job] => Box-Mover
[age] => 35
)
Person 2: Array
(
[name] => John
[job] => Lever-Puller
[age] => 42
)

,
. , , ,
, .
, . ,
.
.


, .
<?php
class Person
{
private $_name;
private $_job;
private $_age;
public function __construct($name, $job, $age)
{
$this->_name = $name;
$this->_job = $job;
$this->_age = $age;
}
public function changeJob($newjob)
{
$this->_job = $newjob;
}
public function happyBirthday()
{

PHP jQuery.indb 109

26.10.2010 14:13:05

110

II. PHP
++$this->_age;

}
}
//
$person1 = new Person("", "Button-Pusher", 34);
$person2 = new Person("", "Lever Puller", 41);
//
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
//
$person1->changeJob("Box-Mover");
$person1->happyBirthday();
//
$person2->happyBirthday();
//
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
?>

.
Person 1: Person Object
(
[_name:private] =>
[_job:private] => Button-Pusher
[_age:private] => 34
)
Person 2: Person Object
(
[_name:private] =>
[_job:private] => Lever Puller
[_age:private] => 41
)
Person 1: Person Object
(
[_name:private] =>
[_job:private] => Box-Mover
[_age:private] => 35
)
Person 2: Person Object
(
[_name:private] =>
[_job:private] => Lever Puller
[_age:private] => 42
)

PHP jQuery.indb 110

26.10.2010 14:13:05

3. -

111

- , , , , , , :

, .
, ,
, , , .
. -. - ,
. - , .


, ,
. ,
,
.
, , 150 ,
, . 150
class._.php inc .
150 - , PHP __autoload(), ,
, , .
<?php
function __autoload($class_name)
{
include_once 'inc/class.' . $class_name . '.inc.php';
}
?>

,
, .


- , , ,
, . , ( )
,
.

PHP jQuery.indb 111

26.10.2010 14:13:05

112

II. PHP

-
.
-
DRY-. ,
,
- . , , , .

-
.
, , .

PHP jQuery.indb 112

26.10.2010 14:13:06

, -
, , .
, PHP jQuery.


, , ,
. (MySQL),
:
() , .


, , . ,
.
,
.
 event_id. , .
 event_title. .
 event_desc. .
 event_start. ( -- ::).
 event_end. ( --
::).

PHP jQuery.indb 113

26.10.2010 14:13:06

114

II. PHP


, , .
Calendar; .
 .

: , , , ,
, .

 HTML-, - .

, .

,
.

 , .
 .
 .

,
.

 HTML- : ,
.
 HTML-: (ID) .


, ,
.
, , (document root) . ,
, , . ,
, , .
: public,
, ,
CSS, index.php JavaScript, sys, , ,
, PHP- .

PHP jQuery.indb 114

26.10.2010 14:13:06

4.

115


- public. URL .
,
.
 index.php. , , .
 view.php. , .
 admin.php.
.
 confirmdelete.php. ,
,
.
public assets,
, .
,
: , CSS-, JavaScript , .
assets : common, css, inc js.
common ,
( , HTML- ),
css , inc ,
, js JavaScript.


sys : class (, Calendar), config , ,
core , .
,
,
(. 4.1).

. 4.1.

PHP jQuery.indb 115

26.10.2010 14:13:06

116

II. PHP

?
, : ? ?
, , . , , (, ) (IP- URL,
IP-). - ,
.
,
.. , , ,
. , , .
, , , , .
, .
, , ,
.



, , : , ,
.
, public.
. sys public ( ,
). , , . ,
.


( public),
.
, , XAMPP,
Apache, httpd.conf ( : /xamppfiles/etc/httpd.conf
Mac, /opt/lampp/etc/httpd.conf Linux \xampp\apache\conf\httpd.conf
Windows).
httpd.conf DocumentRoot. public. , .
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory,
# but symbolic links and aliases may be used to point to other
# locations.
#
DocumentRoot "C:/XAMPP/xampp/htdocs/public"

PHP jQuery.indb 116

26.10.2010 14:13:06

4.

117

httpd.conf , , . .
<Directory "C:/XAMPP/xampp/htdocs/public">

, , , Apache,
XAMPP.
public .
, index.php :
<?php echo " !"; ?>

- ( localhost), (. 4.2).

. 4.2. index.php public Apache


, ,
public , , .
- . , public, .
. , sys public,
.


, . -

PHP jQuery.indb 117

26.10.2010 14:13:06

118

II. PHP

, ( ) ( ). .


, ,
, , . XAMPP phpMyAdmin (http://localhost/
phpmyadmin) SQL ( PHP- phpMyAdmin).
php-jquery_example, events
, SQL.
CREATE DATABASE IF NOT EXISTS `php-jquery_example`
DEFAULT CHARACTER SET utf8
COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `php-jquery_example`.`events` (
`event_id` INT(11) NOT NULL AUTO_INCREMENT,
`event_title` VARCHAR(80) DEFAULT NULL,
`event_desc` TEXT,
`event_start` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`event_end` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`event_id`),
INDEX (`event_start`)
) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci;
INSERT INTO `php-jquery_example`.`events`
(`event_title`, `event_desc`, `event_start`, `event_end`) VALUES
('New Year&#039;s Day', 'Happy New Year!',
'2010-01-01 00:00:00', '2010-01-01 23:59:59'),
('Last Day of January', 'Last day of the month! Yay!',
'2010-01-31 00:00:00', '2010-01-31 23:59:59');
. MySQL. jQuery PHP, , MySQL, .
MySQL PHP MySQL. , 2- (,
2010 .).


php-jquery_example. , , events
(. 4.3).


, ,
. DB_Connect
class.db_connect.inc.php, class (/sys/class/
class.db_connect.inc.php).

PHP jQuery.indb 118

26.10.2010 14:13:06

4.

119

. 4.3. ,

, .
$db .
. , $db, PDO (PHP Data Object).
class.db_connect.inc.php .
<?php
/**
* ( ,
* ..)
*
* PHP 5
*
* : MIT,
* :
* http://www.opensource.org/licenses/mit-license.html
*
* @author
Jason Lengstorf <jason.lengstorf@ennuidesign.com>
* @copyright 2009 Ennui Design
* @license
http://www.opensource.org/licenses/mit-license.html
*/
class DB_Connect {
/**

PHP jQuery.indb 119

26.10.2010 14:13:06

120

II. PHP

*
*
* @var object:
*/
protected $db;
/**
* , -*
*
* @param object $dbo:
*/
protected function __construct($db=NULL)
{
if ( is_object($db) )
{
$this->db = $db;
}
else
{
//

//
/sys/config/db-cred.inc.php
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
try
{
$this->db = new PDO($dsn, DB_USER, DB_PASS);
}
catch ( Exception $e )
{
// ,
//
die ( $e->getMessage() );
}
}
}
}
?>
. , .
.

-
class.calendar.inc.
php, sys (/sys/class/class.
calendar.inc.php). , DB_Connect.
Calendar, .
<?php
/**

PHP jQuery.indb 120

26.10.2010 14:13:06

4.

121

*
*
* PHP 5
*
* : MIT,
* :
* http://www.opensource.org/licenses/mit-license.html
*
* @author
Jason Lengstorf <jason.lengstorf@ennuidesign.com>
* @copyright 2009 Ennui Design
* @license
http://www.opensource.org/licenses/mit-license.html
*/
class Calendar extends DB_Connect
{
//
}
?>

, .


Calendar ,
, , , .
Calendar
.
<?php
class Calendar extends DB_Connect
{
/**
* ,
*
* : -- ::
*
* @var string: ,
*/
private $_useDate;
/**
* ,
*
* @var int:
*/
private $_m;
/**
* ,
*
* @var int:
*/

PHP jQuery.indb 121

26.10.2010 14:13:06

122

II. PHP

private $_y;
/**
*
*
* @var int:
*/
private $_daysInMonth;
/**
* , (0-6)
*
* @var int: ,
*/
private $_startDay;
//
}
?>
. Doc- .

,
, .
 $_useDate. -- ::, .
 $_useDate. , .
 $_useDate. , .
 $_useDate. .
 $_useDate. 0 6, ,
.


. .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;

PHP jQuery.indb 122

26.10.2010 14:13:06

4.

123

/**
*
*
*
* .
* null,
* $_db. null,
* PDO-.
*
*
* , ,
* , ,
* , .
*
* @param object $dbo:
* @param string $useDate: ,
* @return void
*/
public function __construct($dbo=NULL, $useDate=NULL)
{
}
}
?>

:
, , .


,
.
DB_Connect ,
, ,
.
, .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;

PHP jQuery.indb 123

26.10.2010 14:13:06

124

II. PHP

private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL)
{
/*
*
*
*/
parent::__construct($dbo);
}
}
?>


, , , . db_cred.inc.php config (/sys/config/db-cred.inc.php).
, $C ( )
-.
<?php
/*
*
*/
$C = array();
/*
* URL-
*/
$C['DB_HOST'] = 'localhost';
/*
*
*/
$C['DB_USER'] = 'root';
/*
*
*/
$C['DB_PASS'] = '';
/*
*
*/
$C['DB_NAME'] = 'php-jquery_example';
?>
. $C ,
, , $C
. ,
.

PHP jQuery.indb 124

26.10.2010 14:13:06

4.

125

. XAMPP
, ,
, , , .


. .
, .
, .
init.inc.php core (/sys/core/init.inc.php)
.
<?php
/*
*
*/
include_once '../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* PDO-
*/
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
$dbo = new PDO($dsn, DB_USER, DB_PASS);
/*
*
*/
function __autoload($class)
{
$filename = "../sys/class/class." . $class . ".inc.php";
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>

, , . , . http://php.net/autoload.

PHP jQuery.indb 125

26.10.2010 14:13:06

126

II. PHP


, , index.php, public. Calendar. , , , , , .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
if ( is_object ($cal) )
{
echo "<pre>", var_dump($cal), "</pre>";
}
?>

http://localhost/, .
object(Calendar)#2 (6) {
["_useDate":"Calendar":private]=>
NULL
["_m":"Calendar":private]=>
NULL
["_y":"Calendar":private]=>
NULL
["_daysInMonth":"Calendar":private]=>
NULL
["_startDay":"Calendar":private]=>
NULL
["db":protected]=>
object(PDO)#1 (0) {
}
}


,
Calendar .
, , .
, ; , $_useDate, .

PHP jQuery.indb 126

26.10.2010 14:13:06

4.

127

UNIX (
UNIX1;
http://ru.wikipedia.org/wiki/UNIX-), , $_m $_y.
, , $_m $_y, ,
, .

, :
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL)
{
/*
*
*
*/
parent::__construct($dbo);
/*
* ,
*/
if ( isset($useDate) )
{
$this->_useDate = $useDate;
}
else
{
$this->_useDate = date('Y-m-d H:i:s');
}
/*
* UNIX,
* ,
*
1
UNIX ( UTC) 31 1969 1 1970 . . .

PHP jQuery.indb 127

26.10.2010 14:13:06

128

II. PHP
*/
$ts = strtotime($this->_useDate);
$this->_m = date('m', $ts);
$this->_y = date('Y', $ts);
/*
* ,
*/
$this->_daysInMonth = cal_days_in_month(
CAL_GREGORIAN,
$this->_m,
$this->_y
);
/*
* ,
*/
$ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
$this->_startDay = date('w', $ts);

}
}
?>

http://localhost/ ,
NULL, .
object(Calendar)#2 (6) {
["_useDate":"Calendar":private]=>
string(19) "2010-01-01 12:00:00"
["_m":"Calendar":private]=>
string(2) "01"
["_y":"Calendar":private]=>
string(4) "2010"
["_daysInMonth":"Calendar":private]=>
int(31)
["_startDay":"Calendar":private]=>
string(1) "5"
["db":protected]=>
object(PDO)#1 (0) {
}
}


, , ,
. (
),
, .
_loadEventData() . (ID)
, .

PHP jQuery.indb 128

26.10.2010 14:13:06

4.

129

1. SELECT
events.
2. , , , WHERE,
.
3. :

,
11:59:59 (PM) ;

WHERE...BETWEEN , , ,
.

4. .
5. .

.
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
/**
* ()
*
* @param int $id: (ID),
*
* @return array: ,
*/
private function _loadEventData($id=NULL)
{
$sql = "SELECT
`event_id`, `event_title`, `event_desc`,
`event_start`, `event_end`
FROM `events`";
/*
* (ID) ,
* WHERE,
*/

PHP jQuery.indb 129

26.10.2010 14:13:06

130

II. PHP
if ( !empty($id) )
{
$sql .= "WHERE `event_id`=:id LIMIT 1";
}
/*
* ,
*
*/
else
{
/*
*
*/
$start_ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
$end_ts = mktime(23, 59, 59, $this->_m+1, 0, $this->_y);
$start_date = date('Y-m-d H:i:s', $start_ts);
$end_date = date('Y-m-d H:i:s', $end_ts);
/*
* , ,
* ,
*/
$sql .= "WHERE `event_start`
BETWEEN '$start_date'
AND '$end_date'
ORDER BY `event_start`";
}
try
{
$stmt = $this->db->prepare($sql);
/*
* ,
*/
if ( !empty($id) )
{
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
}
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
return $results;
}
catch ( Exception $e )
{
die ( $e->getMessage() );
}

}
}
?>

PHP jQuery.indb 130

26.10.2010 14:13:07

4.

131

. , , .

, , , .
array(2) {
[0]=>
array(5) {
["event_id"]=>
string(1) "1"
["event_title"]=>
string(19) "New Year's Day"
["event_desc"]=>
string(15) "Happy New Year!"
["event_start"]=>
string(19) "2010-01-01 00:00:00"
["event_end"]=>
string(19) "2010-01-01 23:59:59"
}
[1]=>
array(5) {
["event_id"]=>
string(1) "2"
["event_title"]=>
string(19) "Last Day of January"
["event_desc"]=>
string(27) "Last day of the month! Yay!"
["event_start"]=>
string(19) "2010-01-31 00:00:00"
["event_end"]=>
string(19) "2010-01-31 23:59:59"
}
}


_loadEventData() .
,
_loadEventData ,
. , , .
,
, .
.
array(2) {
[1]=>
array(1) {
[0]=>
object(Event)#3 (5) {
["id"]=>
string(1) "1"
["title"]=>

PHP jQuery.indb 131

26.10.2010 14:13:07

132

II. PHP
string(19) "New Year's Day"
["description"]=>
string(15) "Happy New Year"
["start"]=>
string(19) "2010-01-01 00:00:00"
["end"]=>
string(19) "2010-01-01 23:59:59"

}
}
[31]=>
array(1) {
[0]=>
object(Event)#4 (5) {
["id"]=>
string(1) "2"
["title"]=>
string(19) "Last Day of January"
["description"]=>
string(27) "Last day of the month! Yay!"
["start"]=>
string(19) "2010-01-31 00:00:00"
["end"]=>
string(19) "2010-01-31 23:59:59"
}
}
}


,
Event class (/sys/class/class.event.inc.
php). : $id, $title, $description, $start
$end, , , .
.
<?php
/**
*
*
* PHP 5
*
* : MIT,
* :
* http://www.opensource.org/licenses/mit-license.html
*
* @author
Jason Lengstorf <jason.lengstorf@ennuidesign.com>
* @copyright 2009 Ennui Design
* @license
http://www.opensource.org/licenses/mit-license.html
*/
class Event
{

PHP jQuery.indb 132

26.10.2010 14:13:07

4.

133

/**
* (ID)
*
* @var int
*/
public $id;
/**
*
*
* @var string
*/
public $title;
/**
*
*
* @var string
*/
public $description;
/**
*
*
* @var string
*/
public $start;
/**
*
*
* @var string
*/
public $end;
/**
*
*
* @param array $event:
* @return void
*/
public function __construct($event)
{
if ( is_array($event) )
{
$this->id = $event['event_id'];
$this->title = $event['event_title'];
$this->description = $event['event_desc'];
$this->start = $event['event_start'];
$this->end = $event['event_end'];
}
else
{
throw new Exception(" .");

PHP jQuery.indb 133

26.10.2010 14:13:07

134

II. PHP
}

}
}
?>

,
, , ,
. _loadEventData().
, . Calendar
, _createEventObj().
, .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
private function _loadEventData($id=NULL) {...}
/**
* , ,
*
* @return array:
*/
private function _createEventObj()
{
/*
*
*/
$arr = $this->_loadEventData();
/*
* ,
* ,
*/
$events = array();

PHP jQuery.indb 134

26.10.2010 14:13:07

135

4.
foreach ( $arr as $event )
{
$day = date('j', strtotime($event['event_start']));
try
{
$events[$day][] = new Event($event);
}
catch ( Exception $e )
{
die ( $e->getMessage() );
}
}
return $events;
}
}
?>

, ,
, HTML.

HTML-
, , . .
buildCalendar(). , :
 , ;
 , ;
 , ,
.
buildCalendar() Calendar H2. ,
, .
,
.
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;

PHP jQuery.indb 135

26.10.2010 14:13:07

136

II. PHP

private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
/**
* HTML-
*
* , ,
* ,
* .
*
* @return string: HTML-
*/
public function buildCalendar()
{
/*
*
* ,
*
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat');
/*
* HTML-
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
}
$html .= "\n\t<ul class=\"weekdays\">"
. $labels . "\n\t</ul>";
/*
* HTML-
*/
return $html;
}
}
?>

index.php
buildCalendar,
index.php, public, .
, , .

PHP jQuery.indb 136

26.10.2010 14:13:07

4.

137

<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/*
* HTML
*/
echo $cal->buildCalendar();
?>

, (. 4.4).

. 4.4.



. , .
1. .
2. ( ,
, , ),

PHP jQuery.indb 137

26.10.2010 14:13:07

138

II. PHP
,
.

3. fill , .
4. today, ,
, .
5.
.
6. , ,
, .
7. , , ,
.
8. .
9. , .
10. .
. 1 2, buildCalendar() , .
public function buildCalendar()
{
/*
*
* ,
*
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat');
/*
* HTML-
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
}
$html .= "\n\t<ul class=\"weekdays\">"
. $labels . "\n\t</ul>";
/*
* HTML-
*/
$html .= "\n\t<ul>"; //
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
$c<=$this->_daysInMonth; ++$i )
{
//
}
/*

PHP jQuery.indb 138

26.10.2010 14:13:07

4.

139

* HTML-
*/
return $html;
}

, . 35, , .
public function buildCalendar()
{
/*
*
* ,
*
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat');
/*
* HTML-
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
}
$html .= "\n\t<ul class=\"weekdays\">" . $labels .
"\n\t</ul>";
/*
* HTML-
*/
$html .= "\n\t<ul>"; // Start a new unordered list
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
$c<=$this->_daysInMonth; ++$i )
{
/*
* "fill" ,
*
*/
$class = $i<=$this->_startDay ? "fill" : NULL;
/*
* "today",
*/
if ( $c==$t && $m==$this->_m && $y==$this->_y )
{
$class = "today";
}
/*
*
*
*/

PHP jQuery.indb 139

26.10.2010 14:13:07

140

II. PHP
$ls = sprintf("\n\t\t<li class=\"%s\">", $class);
$le = "\n\t\t</li>";
//
}
/*
* HTML-
*/
return $html;

, , . 610, , .
public function buildCalendar()
{
/*
*
* ,
*
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat');
/*
* HTML-
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
}
$html .= "\n\t<ul class=\"weekdays\">" . $labels .
"\n\t</ul>";
/*
* HTML-
*/
$html .= "\n\t<ul>"; // Start a new unordered list
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
$c<=$this->_daysInMonth; ++$i )
{
/*
* "fill" ,
*
*/
$class = $i<=$this->_startDay ? "fill" : NULL;
/*
* "today",
*/
if ( $c==$t && $m==$this->_m && $y==$this->_y )
{

PHP jQuery.indb 140

26.10.2010 14:13:07

4.

141

$class = "today";
}
/*
*
*
*/
$ls = sprintf("\n\t\t<li class=\"%s\">", $class);
$le = "\n\t\t</li>";
/*
* ,
*
*/
if ( $this->_startDay<$i && $this->_daysInMonth>=$c)
{
$date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);
}
else { $date="&nbsp;"; }
/*
* ,
*/
$wrap = $i!=0 && $i%7==0 ? "\n\t</ul>\n\t<ul>" : NULL;
/*
*
*/
$html .= $ls . $date . $le . $wrap;
}
/*
*
*/
while ( $i%7!=1 )
{
$html .= "\n\t\t<li class=\"fill\">&nbsp;</li>";++$i;
}
/*
*
*/
$html .= "\n\t</ul>\n\n";
/*
* HTML-
*/
return $html;
}

,
(. 4.5).

PHP jQuery.indb 141

26.10.2010 14:13:07

142

II. PHP

. 4.5. , buildCalendar()



events _createEventObj()
, , .
, .
public function buildCalendar()
{
/*
*
* ,
*
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu',
'Fri', 'Sat');
/*
* HTML-
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{

PHP jQuery.indb 142

26.10.2010 14:13:07

4.

143

$labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";


}
$html .= "\n\t<ul class=\"weekdays\">" . $labels .
"\n\t</ul>";
/*
*
*/
$events = $this->_createEventObj();
/*
* HTML-
*/
$html .= "\n\t<ul>"; // Start a new unordered list
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
$c<=$this->_daysInMonth; ++$i )
{
/*
* "fill" ,
*
*/
$class = $i<=$this->_startDay ? "fill" : NULL;
/*
* "today",
*/
if ( $c==$t && $m==$this->_m && $y==$this->_y )
{
$class = "today";
}
/*
*
*
*/
$ls = sprintf("\n\t\t<li class=\"%s\">", $class);
$le = "\n\t\t</li>";
/*
* ,
*
*/
if ( $this->_startDay<$i && $this->_daysInMonth>=$c)
{
/*
*
*/
$event_info = NULL; // clear the variable
if ( isset($events[$c]) )
{
foreach ( $events[$c] as $event )
{
$link = '<a href="view.php?event_id='
. $event->id . '">' . $event->title
. '</a>';

PHP jQuery.indb 143

26.10.2010 14:13:07

144

II. PHP
$event_info .= "\n\t\t\t$link";
}
}
$date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);
}
else { $date="&nbsp;"; }
/*
* ,
*/
$wrap = $i!=0 && $i%7==0 ? "\n\t</ul>\n\t<ul>" : NULL;
/*
*
*/
$html .= $ls . $date . $event_info . $le . $wrap;
}
/*
*
*/
while ( $i%7!=1 )
{
$html .= "\n\t\t<li class=\"fill\">&nbsp;</li>";++$i;
}
/*
*
*/
$html .= "\n\t</ul>\n\n";
/*
* HTML-
*/
return $html;

}
. $event_info
.

, (. 4.6).
. view.php, . .


, ,
, .
HTML, CSS.

PHP jQuery.indb 144

26.10.2010 14:13:07

4.

145

. 4.6.
. , CSS . CSS HTML, XHTML CSS. , 5-
(, 2010 .).

, CSS- :
 ;
 , ,
;
 , , , ;
 ;
 ;
 CSS3,
, .
. CSS3 http://css3.info/.

css style.css (/public/assets/css/style.


css) CSS.
body {
background-color: #789;
font-family: georgia, serif;
font-size: 13px;
}
#content {

PHP jQuery.indb 145

26.10.2010 14:13:07

146

II. PHP

display: block;
width: 812px;
margin: 40px auto 10px;
padding: 10px;
background-color: #FFF;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border:2px solid black;
-moz-box-shadow: 0 0 14px #123;
-webkit-box-shadow: 0 0 14px #123;
box-shadow: 0 0 14px #123;
}
h2,p {
margin: 0 auto 14px;
text-align: center;
}
ul {
display: block;
clear: left;
height: 82px;
width: 812px;
margin: 0 auto;
padding: 0;
list-style: none;
background-color: #FFF;
text-align: center;
border: 1px solid black;
border-top: 0;
border-bottom: 2px solid black;
}
li {
position: relative;
float: left;
margin: 0;
padding: 20px 2px 2px;
border-left: 1px solid black;
border-right: 1px solid black;
width: 110px;
height: 60px;
overflow: hidden;
background-color: white;
}
li:hover {
background-color: #FCB;
z-index: 1;
-moz-box-shadow: 0 0 10px #789;
-webkit-box-shadow: 0 0 10px #789;
box-shadow: 0 0 10px #789;
}

PHP jQuery.indb 146

26.10.2010 14:13:07

4.

147

.weekdays {
height: 20px;
border-top: 2px solid black;
}
.weekdays li {
height: 16px;
padding: 2px 2px;
background-color: #BCF;
}
.fill {
background-color: #BCD;
}
.weekdays li:hover,li.fill:hover {
background-color: #BCD;
-moz-box-shadow: none;
-webkit-box-shadow: none;
box-shadow: none;
}
.weekdays li:hover,.today {
background-color: #BCF;
}
li strong {
position: absolute;
top: 2px;
right: 2px;
}
li a {
position: relative;
display: block;
border: 1px dotted black;
margin: 2px;
padding: 2px;
font-size: 11px;
background-color: #DEF;
text-align: left;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
z-index: 1;
text-decoration: none;
color: black;
font-weight: bold;
font-style: italic;
}
li a:hover {
background-color: #BCF;
z-index: 2;

PHP jQuery.indb 147

26.10.2010 14:13:07

148

II. PHP

-moz-box-shadow: 0 0 6px #789;


-webkit-box-shadow: 0 0 6px #789;
box-shadow: 0 0 6px #789;
}

;
. , , , .


, , , , ,
HTML-, .. ,
: header.inc.php ( ) footer.inc.php ( ).
header.inc.php, common
(/public/assets/common/header.inc.php).
DOCTYPE HTML , Content-Type, CSS-.
,
$page_title.
, CSS-,
$css_files .
header.inc.php .
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title><?php echo $page_title; ?></title>
<?php foreach ( $css_files as $css ): ?>
<link rel="stylesheet" type="text/css" media="screen,projection"
href="assets/css/<?php echo $css; ?>" />
<?php endforeach; ?>
</head>
<body>

common footer.inc.php (/public/assets/


common/footer.inc.php), .
body html, header.inc.php, .
footer.inc.php .
</body>
</html>

PHP jQuery.indb 148

26.10.2010 14:13:07

4.

149

index.php
, index.php. header.
inc.php $page_title $css_files.
, ,
div content,
buildCalendar().
, footer.inc.php .
, index.php , .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/*
* CSS
*/
$page_title = " ";
$css_files = array('style.css');
/*
*
*/
include_once 'assets/common/header.inc.php';
?>
<div id="content">
<?php
/*
* HTML
*/
echo $cal->buildCalendar();
?>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

PHP jQuery.indb 149

26.10.2010 14:13:07

150

II. PHP

, (. 4.7).

. 4.7. ,
CSS-

HTML-

, , .
.
1. , .
2. , ,
.
3. ,
.

PHP jQuery.indb 150

26.10.2010 14:13:07

4.

151


_LoadEventByID(), _create
EventObj(), Event , _loadEventData().
, , ,
(ID) _loadEventData()
( , LIMIT 1
_loadEventData()) .
Calendar , .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
/**
*
*
* @param int $id: (ID)
* @return object:
*/
private function _loadEventById($id)
{
/*
* ID , NULL
*/
if ( empty($id) )
{
return NULL;
}
/*
*
*/

PHP jQuery.indb 151

26.10.2010 14:13:07

152

II. PHP
$event = $this->_loadEventData($id);
/*
*
*/
if ( isset($event[0]) )
{
return new Event($event[0]);
}
else
{
return NULL;
}

}
}
?>

, ( ID, 1) .
Event Object
(
[id] => 1
[title] => New Year's Day
[description] => Happy New Year!
[start] => 2010-01-01 00:00:00
[end] => 2010-01-01 23:59:59


, ,
, ,
HTML-.
displayEvent(). HTML-, :
1) _loadEventById();
2) ,
;
3) HTML- .
displayEvent(), Calendar , .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;

PHP jQuery.indb 152

26.10.2010 14:13:08

4.

153

private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
/**
*
*
* @param int $id: (ID)
* @return string:
*

*/
public function displayEvent($id)
{
/*
* , ID
*/
if ( empty($id) ) { return NULL; }
/*
* , ID
*/
$id = preg_replace('/[^0-9]/', '', $id);
/*
*
*/
$event = $this->_loadEventById($id);
/*
* ,
*/
$ts = strtotime($event->start);
$date = date('F d, Y', $ts);
$start = date('g:ia', $ts);
$end = date('g:ia', strtotime($event->end));
/*
*
*/
return "<h2>$event->title</h2>"
. "\n\t<p class=\"dates\">$date, $start&mdash;$end</p>"
. "\n\t<p>$event->description</p>";
}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}

PHP jQuery.indb 153

26.10.2010 14:13:08

154

II. PHP

private function _loadEventById($id) {...}


}
?>


, displayEvent(), . view.php public (public/view.php).
, (ID) . , .
view.php , CSS- ,
, Calendar.
div content
displayEvent.
, div .
, ,
.
<?php
/*
* , ID
*/
if ( isset($_GET['event_id']) )
{
/*
* , ID
*/
$id = preg_replace('/[^0-9]/', '', $_GET['event_id']);
/*
* ID
*
*/
if ( empty($id) )
{
header("Location: ./");
exit;
}
}
else
{
/*
* ID ,
*
*/
header("Location: ./");
exit;
}

PHP jQuery.indb 154

26.10.2010 14:13:08

4.

155

/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$page_title = " ";
$css_files = array("style.css");
include_once 'assets/common/header.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
?>
<div id="content">
<?php echo $cal->displayEvent($id) ?>
<a href="./">&laquo; </a>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

,
. view.php , (. 4.8).

. 4.8. ,

PHP jQuery.indb 155

26.10.2010 14:13:08

156

II. PHP

, PHP MySQL. ,
HTML- .
, , .

PHP jQuery.indb 156

26.10.2010 14:13:08

, ,
, c , , , .

,
. Calendar displayForm().
:
 , , ;
 , ;
 , ;
 ,
;
 .
. , , ,
$_POST, ,
0.

displayForm(), Calendar , .
<?php
class Calendar extends DB_Connect
{
private $_useDate;

PHP jQuery.indb 157

26.10.2010 14:13:08

158

II. PHP

private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
/**
* ,
*
*
* @return string: HTML-
*
*/
public function displayForm()
{
/*
* , (ID)
*/
if ( isset($_POST['event_id']) )
{
$id = (int) $_POST['event_id'];
//
//
}
else
{
$id = NULL;
}
/*
* ,
*
*/
$submit = " ";
/*
* ID,
*/
if ( !empty($id) )
{
$event = $this->_loadEventById($id);
/*
* , NULL
*/
if ( !is_object($event) ) { return NULL; }

PHP jQuery.indb 158

26.10.2010 14:13:08

5. ,

159

$submit = " ";


}
/*
*
*/
return <<<FORM_MARKUP
<form action="assets/inc/process.inc.php" method="post">
<fieldset>
<legend>$submit</legend>
<label for="event_title"> </label>
<input type="text" name="event_title"
id="event_title" value="$event->title" />
<label for="event_start"> </label>
<input type="text" name="event_start"
id="event_start" value="$event->start" />
<label for="event_end"> </label>
<input type="text" name="event_end"
id="event_end" value="$event->end" />
<label for="event_description"> </label>
<textarea name="event_description"
id="event_description">$event->description
</textarea>
<input type="hidden" name="event_id" value="$event->id" />
<input type="hidden" name="token" value="$_SESSION[token]" />
<input type="hidden" name="action" value="event_edit" />
<input type="submit" name="event_submit" value="$submit" />
or <a href="./">cancel</a>
</fieldset>
</form>
FORM_MARKUP;
}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
}
?>


, token, (token). , (cross-site request forgeries, CSRF),
,
, . , -

PHP jQuery.indb 159

26.10.2010 14:13:08

160

II. PHP

,
.
CSRF - .
, $_POST, c , $_SESSION, .
, , .
<?php
/*
*
*/
session_start();
/*
* CSRF,
*
*/
if ( !isset($_SESSION['token']) )
{
$_SESSION['token'] = sha1(uniqid(mt_rand(), TRUE));
}
/*
*
*/
include_once '../sys/config/db-cred.inc.php'; //
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* PDO-
*/
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
$dbo = new PDO($dsn, DB_USER, DB_PASS);
/*
*
*/
function __autoload($class)
{
$filename = "../sys/class/class." . $class . ".inc.php";
if ( file_exists($filename) )
{

PHP jQuery.indb 160

26.10.2010 14:13:09

5. ,

161

include_once $filename;
}
}
?>
.
. , , 20 ,
, . CSRF
(Chris Shiflett) http://
shiflett.org/csrf


, , ,
. admin.php public (/public/
admin.php).
view.php, :
 ;
 CSS-;
 ;
 Calendar;
 displayForm();
 .
admin.php .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$page_title = "/ ";
$css_files = array("style.css");
include_once 'assets/common/header.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
?>
<div id="content">
<?php echo $cal->displayForm(); ?>

PHP jQuery.indb 161

26.10.2010 14:13:09

162

II. PHP

</div><!-- end #content -->


<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

, http://localhost/
admin.php, (. 5.1).

. 5.1. CSS-

, .
(
,
), CSS- , admin.css css
(/public/assets/css/).
,
, CSS . , CSS , ; , , , .
admin.css .
fieldset {
border: 0;
}
legend {
font-size: 24px;
font-weight: bold;
}

PHP jQuery.indb 162

26.10.2010 14:13:09

5. ,

163

input[type=text],input[type=password],label {
display: block;
width: 70%;
font-weight: bold;
}
textarea {
width: 99%;
height: 200px;
}
input[type=text],input[type=password],textarea {
border: 1px solid #123;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: inset 1px 2px 4px #789;
-webkit-box-shadow: inset 1px 2px 4px #789;
box-shadow: inset 1px 2px 4px #789;
padding: 4px;
margin: 0 0 4px;
font-size: 16px;
font-family: georgia, serif;
}
input[type=submit] {
margin: 4px 0;
padding: 4px;
border: 1px solid #123;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: inset -2px -1px 3px #345,
inset 1px 1px 3px #BCF,
1px 2px 6px #789;
-webkit-box-shadow: inset -2px -1px 3px #345,
inset 1px 1px 3px #BCF,
1px 2px 6px #789;
box-shadow: inset -2px -1px 3px #345,
inset 1px 1px 3px #BCF,
1px 2px 6px #789;
background-color: #789;
font-family: georgia, serif;
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
text-shadow: 0px 0px 1px #fff;
}
.admin-options {
text-align: center;
}
.admin-options form,.admin-options p {

PHP jQuery.indb 163

26.10.2010 14:13:09

164

II. PHP

display: inline;
}
a.admin {
display: inline-block;
margin: 4px 0;
padding: 4px;
border: 1px solid #123;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: inset -2px -1px 3px #345,
inset 1px 1px 3px #BCF,
1px 2px 6px #789;
-webkit-box-shadow: inset -2px -1px 3px #345,
inset 1px 1px 3px #BCF,
1px 2px 6px #789;
box-shadow: inset -2px -1px 3px #345,
inset 1px 1px 3px #BCF,
1px 2px 6px #789;
background-color: #789;
color: black;
text-decoration: none;
font-family: georgia, serif;
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
text-shadow: 0px 0px 1px #fff;
}

, admin.css $css_files
admin.php, , .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$page_title = "/ ";
$css_files = array("style.css", "admin.css");
include_once 'assets/common/header.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
?>
<div id="content">

PHP jQuery.indb 164

26.10.2010 14:13:09

5. ,

165

<?php echo $cal->displayForm(); ?>


</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

http://localhost/admin.php,
, (. 5.2).

. 5.2. CSS


, , Calendar
processForm(), :
 ,
POST;
 ;

PHP jQuery.indb 165

26.10.2010 14:13:09

166

II. PHP

 INSERT, ,
UPDATE, ;
 ;
 TRUE , ,
.
processForm(), .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
public function displayForm() {...}
/**
*
*
*
* @return mixed: TRUE
*

*/
public function processForm()
{
/*
* , "action"
*/
if ( $_POST['action']!='event_edit' )
{
return " processForm";
}
/*
*
*/
$title = htmlentities($_POST['event_title'], ENT_QUOTES);
$desc = htmlentities($_POST['event_description'], ENT_QUOTES);

PHP jQuery.indb 166

26.10.2010 14:13:10

5. ,

167

$start = htmlentities($_POST['event_start'], ENT_QUOTES);


$end = htmlentities($_POST['event_end'], ENT_QUOTES);
/*
* ID ,
*/
if ( empty($_POST['event_id']) )
{
$sql = "INSERT INTO `events`
(`event_title`, `event_desc`, `event_start`,
`event_end`)
VALUES
(:title, :description, :start, :end)";
}
/*
* ,
*/
else
{
/*
* ID
*
*/
$id = (int) $_POST['event_id'];
$sql = "UPDATE `events`
SET
`event_title`=:title,
`event_desc`=:description,
`event_start`=:start,
`event_end`=:end
WHERE `event_id`=$id";
}
/*
*
*
*/
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(":title", $title, PDO::PARAM_STR);
$stmt->bindParam(":description", $desc, PDO::PARAM_STR);
$stmt->bindParam(":start", $start, PDO::PARAM_STR);
$stmt->bindParam(":end", $end, PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
return TRUE;
}
catch ( Exception $e )
{
return $e->getMessage();
}
}

PHP jQuery.indb 167

26.10.2010 14:13:10

168

II. PHP

private function _loadEventData($id=NULL) {...}


private function _createEventObj() {...}
private function _loadEventById($id) {...}
}
?>

,

, ,
process.inc.php, inc (/
public/assets/inc/process.inc.php). ,
, ,
.
1. .
2. ,
Calendar.
3. ( ).
4. , ,
.
5. , , , ,
, .
. 6, . 7.
6. Calendar:

processForm();

7. ,
, .
. 4 if...elseif .
,
, , , ,
.
process.inc.php
, .
<?php
/*
*
*/
session_start();

PHP jQuery.indb 168

26.10.2010 14:13:10

5. ,

169

/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_edit' => array(
'object' => 'Calendar',
'method' => 'processForm',
'header' => 'Location: ../../'
)
);
/*
* , CSRF
*
*/
if ( $_POST['token']==$_SESSION['token']
&& isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
if ( TRUE === $msg=$obj->$use_array['method']() )
{
header($use_array['header']);
exit;
}
else
{
//
die ( $msg );
}
}
else
{
// /
//
header("Location: ../../");
exit;
}
function __autoload($class_name)
{

PHP jQuery.indb 169

26.10.2010 14:13:10

170

II. PHP

$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>

, http://localhost/admin.php , :
 Dinner Party;
 2010-01-22 17:00:00;
 2010-01-22 19:00:00;
 Five-course meal with wine pairings at John's
house.
(. 5.3).

. 5.3. ( )

PHP jQuery.indb 170

26.10.2010 14:13:10

5. ,

171

,
, admin.php. Calendar
_adminGeneralOptions().
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
public function displayForm() {...}
public function processForm() {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
/**
*
*
* @return string:
*/
private function _adminGeneralOptions()
{
/*
*
*/
return <<<ADMIN_OPTIONS
<a href="admin.php" class="admin">+ </a>
ADMIN_OPTIONS;
}
}
?>

PHP jQuery.indb 171

26.10.2010 14:13:10

172

II. PHP

. ,
, 6.

buildCalendar() ,
_adminGeneralOptions(), ,
.
public function buildCalendar()
{
//
/*
*
*/
$html .= "\n\t</ul>\n\n";
/*
* ,
*/
$admin = $this->_adminGeneralOptions();
/*
*
*/
return $html . $admin;
}

, ,
index.php (admin.
css), , :
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/*
* CSS
*/
$page_title = " ";
$css_files = array('style.css', 'admin.css');
/*
*
*/
include_once 'assets/common/header.inc.php';
?>

PHP jQuery.indb 172

26.10.2010 14:13:10

5. ,

173

<div id="content">
<?php
/*
* HTML
*/
echo $cal->buildCalendar();
?>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

http://localhost/ (. 5.4).

. 5.4.

PHP jQuery.indb 173

26.10.2010 14:13:10

174

II. PHP



. , view.php .
, ,
, .
Calendar _adminEntryOptions(), .

. .
, Calendar, .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
public function displayForm() {...}
public function processForm() {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
private function _adminGeneralOptions() {...}
/**
*
* (ID)

PHP jQuery.indb 174

26.10.2010 14:13:10

5. ,

175

*
* @param int $id: ,
*

* @return string: /
*/
private function _adminEntryOptions($id)
{
return <<<ADMIN_OPTIONS
<div class="admin-options">
<form action="admin.php" method="post">
<p>
<input type="submit" name="edit_event"
value=" " />
<input type="hidden" name="event_id"
value="$id" />
</p>
</form>
</div><!-- end .admin-options -->
ADMIN_OPTIONS;
}
}
?>

displayEvent

, _adminEntryOptions() displayEvent().
_adminEntryOptions() $admin .
displayEvent() Calendar ,
.
/**
*
*
* @param int $id: (ID)
* @return string:
*

*/
public function displayEvent($id)
{
/*
* , ID
*/
if ( empty($id) ) { return NULL; }
/*
* , ID
*/
$id = preg_replace('/[^0-9]/', '', $id);

PHP jQuery.indb 175

26.10.2010 14:13:10

176

II. PHP

/*
*
*/
$event = $this->_loadEventById($id);
/*
* ,
*/
$ts = strtotime($event->start);
$date = date('F d, Y', $ts);
$start = date('g:ia', $ts);
$end = date('g:ia', strtotime($event->end));
/*
* ,
?
*/
$admin = $this->_adminEntryOptions($id);
/*
*
*/
return "<h2>$event->title</h2>"
. "\n\t<p class=\"dates\">$date, $start&mdash;$end</p>"
. "\n\t<p>$event->description</p>$admin";
}
. $admin .
. , , , .



, ,
, admin.css
$css_files view.php.
<?php
/*
* , ID
*/
if ( isset($_GET['event_id']) )
{
/*
* , ID
*/
$id = preg_replace('/[^0-9]/', '', $_GET['event_id']);
/*

PHP jQuery.indb 176

26.10.2010 14:13:10

5. ,

177

* ID
*
*/
if ( empty($id) )
{
header("Location: ./");
exit;
}
}
else
{
/*
* ID ,
*
*/
header("Location: ./");
exit;
}
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$page_title = " ";
$css_files = array("style.css", "admin.css");
include_once 'assets/common/header.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
?>
<div id="content">
<?php echo $cal->displayEvent($id) ?>
<a href="./">&laquo; </a>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

PHP jQuery.indb 177

26.10.2010 14:13:10

178

II. PHP

, , (. 5.5).

. 5.5.

admin.php,
(. 5.6).

. 5.6. ,

PHP jQuery.indb 178

26.10.2010 14:13:10

5. ,

179


. ,
.
,
.
, .
1. , .
2. , .



. _adminEntryOptions()
Calendar , .
/**
*
* (ID)
*
* @param int $id: ,
*

* @return string: /
*/
private function _adminEntryOptions($id)
{
return <<<ADMIN_OPTIONS
<div class="admin-options">
<form action="admin.php" method="post">
<p>
<input type="submit" name="edit_event"
value=" " />
<input type="hidden" name="event_id"
value="$id" />
</p>
</form>
<form action="confirmdelete.php" method="post">
<p>
<input type="submit" name="delete_event"
value=" " />
<input type="hidden" name="event_id"
value="$id" />
</p>
</form>
</div><!-- end .admin-options -->
ADMIN_OPTIONS;
}

PHP jQuery.indb 179

26.10.2010 14:13:10

180

II. PHP

, confirmdelete.php, . , (. 5.7).

. 5.7.

,


, , . Calendar confirmDelete().
,
, .
1. , . , . 2,
. 3.
2. , :

, ;

3. .
, .
<?php
class Calendar extends DB_Connect
{
private $_useDate;

PHP jQuery.indb 180

26.10.2010 14:13:10

5. ,

181

private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
public function displayForm() {...}
public function processForm() {...}
/**
*
* , , .
*
*
* .
*
* .
* ,
* ,
* .
*
* @param int $id: (ID)
* @return mixed:
*
*/
public function confirmDelete($id)
{
/*
* , (ID)
*/
if ( empty($id) ) { return NULL; }
/*
* ,
*/
$id = preg_replace('/[^0-9]/', '', $id);
/*
* ,
* , ,
*
*/
if ( isset($_POST['confirm_delete'])
&& $_POST['token']==$_SESSION['token'] )
{

PHP jQuery.indb 181

26.10.2010 14:13:10

182

II. PHP
/*
* ,
*
*/
if ( $_POST['confirm_delete']==", " )
{
$sql = "DELETE FROM `events`
WHERE `event_id`=:id
LIMIT 1";
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(
":id",
$id,
PDO::PARAM_INT
);
$stmt->execute();
$stmt->closeCursor();
header("Location: ./");
return;
}
catch ( Exception $e )
{
return $e->getMessage();
}
}
/*
* ,
*
*/
else
{
header("Location: ./");
return;
}
}
/*
* ,
*
*/
$event = $this->_loadEventById($id);
/*
* ,
*
*/
if ( !is_object($event) ) { header("Location: ./"); }
return <<<CONFIRM_DELETE

<form action="confirmdelete.php" method="post">

PHP jQuery.indb 182

26.10.2010 14:13:10

5. ,

183

<h2>
"$event->title"?
</h2>
<p><strong> <strong> </strong></
p>
<p>
<input type="submit" name="confirm_delete"
value=", " />
<input type="submit" name="confirm_delete"
value="! !" />
<input type="hidden" name="event_id"
value="$event->id" />
<input type="hidden" name="token"
value="$_SESSION[token]" />
</p>
</form>
CONFIRM_DELETE;
}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
private function _adminGeneralOptions() {...}
private function _adminEntryOptions($id) {...}
}
?>


confirmDelete() confirmdelete.php
public (/public/confirmdelete.php).
index.php :
 , (ID) , $id; , ;
 ;
 Calendar;
 confirmDelete()
$markup;
 $page_title $css_files ;
 , $markup;
 .

PHP jQuery.indb 183

26.10.2010 14:13:10

184

II. PHP

. , confirmDelete() , ,
header() . confirmDelete(), , ,
header(), .
header() http://php.net/header.

confirmdelete.php .
<?php
/*
* , ID
*/
if ( isset($_POST['event_id']) )
{
/*
* ID URL
*/
$id = (int) $_POST['event_id'];
}
else
{
/*
* ID ,
*
*/
header("Location: ./");
exit;
}
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
$markup = $cal->confirmDelete($id);
/*
*
*/
$page_title = " ";
$css_files = array("style.css", "admin.css");
include_once 'assets/common/header.inc.php';
?>
<div id="content">
<?php echo $markup; ?>

PHP jQuery.indb 184

26.10.2010 14:13:10

5. ,

185

</div><!-- end #content -->


<?php
/*
* Output the footer
*/
include_once 'assets/common/footer.inc.php';
?>

,
Dinner Party. (. 5.8).

. 5.8. ,

, (. 5.9).

PHP jQuery.indb 185

26.10.2010 14:13:10

186

II. PHP

. 5.9.


. , , , , ,
.
.
, .

PHP jQuery.indb 186

26.10.2010 14:13:11

, , ,
, , - .
, -
.

,
, . , users, : , , -
.
, http//localhost/
phpmyadmin, SQL .
CREATE TABLE IF NOT EXISTS `php-jquery_example`.`users` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(80) DEFAULT NULL,
`user_pass` VARCHAR(47) DEFAULT NULL,
`user_email` VARCHAR(80) DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE (`user_name`)
) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci;

, php-jquery_example
users (. 6.1).

PHP jQuery.indb 187

26.10.2010 14:13:11

188

II. PHP

. 6.1. users phpMyAdmin



, . login.php
public (/public/login.php).
admin.php, , ,
.
, user_login.
login.php .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$page_title = ", ";
$css_files = array("style.css", "admin.css");
include_once 'assets/common/header.inc.php';

PHP jQuery.indb 188

26.10.2010 14:13:11

6.

189

?>
<div id="content">
<form action="assets/inc/process.inc.php" method="post">
<fieldset>
<legend>, </legend>
<label for="uname"> </label>
<input type="text" name="uname"
id="uname" value="" />
<label for="pword"></label>
<input type="password" name="pword"
id="pword" value="" />
<input type="hidden" name="token"
value="<?php echo $_SESSION['token']; ?>" />
<input type="hidden" name="action"
value="user_login" />
<input type="submit" name="login_submit"
value="" />
<a href="./"></a>
</fieldset>
</form>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

http://localhost/login.
php,
(. 6.2).

. 6.2.

PHP jQuery.indb 189

26.10.2010 14:13:11

190

II. PHP

Admin
, ,
. class admin.inc.php (/sys/
class/class.admin.inc.php). ,
.


, ,
, DB_Connect. ,
$_saltLength, .
,
, ,
. ,
$_saltLength.
, , admin.inc.php .
<?php
/**
*
*
* PHP 5
*
* : MIT,
* :
* http://www.opensource.org/licenses/mit-license.html
*
* @author
Jason Lengstorf <jason.lengstorf@ennuidesign.com>
* @copyright 2009 Ennui Design
* @license
http://www.opensource.org/licenses/mit-license.html
*/
class Admin extends DB_Connect
{
/**
* ,
*
* @var int:
*/
private $_saltLength = 7;
/**
*
*
* @param object $db:
* @param int $saltLength:
*/
public function __construct($db=NULL, $saltLength=NULL)
{
parent::__construct($db);

PHP jQuery.indb 190

26.10.2010 14:13:11

6.

191

/*
* ,
*/
if ( is_int($saltLength) )
{
$this->_saltLength = $saltLength;
}
}
}
?>

,
events, , login.php,
. .
1. ,
.
2.
htmlentities().
3. ,
.
4. $user
, .
5. () , , ,
.
6. - .
7. ,
, TRUE.
. - .

Admin , . 1 2, , .
<?php
class Admin extends DB_Connect
{
private $_saltLength = 7;
public function __construct($db=NULL, $saltLength=NULL) {...}
/**

PHP jQuery.indb 191

26.10.2010 14:13:11

192

II. PHP

*
*
* @return mixed: TRUE , -*
*/
public function processLoginForm()
{
/*
* ,
* ACTION
*/
if ( $_POST['action']!='user_login' )
{
return " processLoginForm
ACTION";
}
/*
*
*/
$uname = htmlentities($_POST['uname'], ENT_QUOTES);
$pword = htmlentities($_POST['pword'], ENT_QUOTES);
// ...
}
}
?>

. 3 4, .
public function processLoginForm()
{
/*
* ,
* ACTION
*/
if ( $_POST['action']!='user_login' )
{
return " processLoginForm
ACTION";
}
/*
*
*/
$uname = htmlentities($_POST['uname'], ENT_QUOTES);
$pword = htmlentities($_POST['pword'], ENT_QUOTES);
/*
* ,
*

PHP jQuery.indb 192

26.10.2010 14:13:11

6.

193

*/
$sql = "SELECT
`user_id`, `user_name`, `user_email`, `user_pass`
FROM `users`
WHERE
`user_name` = :uname
LIMIT 1";
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':uname', $uname, PDO::PARAM_STR);
$stmt->execute();
$user = array_shift($stmt->fetchAll());
$stmt->closeCursor();
}
catch ( Exception $e )
{
die ( $e->getMessage() );
}
/*
* ,
*
*/
if ( !isset($user) )
{
return " .";
}
// ...
}

$user ( - , users).
. 57; .
public function processLoginForm()
{
/*
* ,
* ACTION
*/
if ( $_POST['action']!='user_login' )
{
return " processLoginForm
ACTION";
}
/*
*
*/
$uname = htmlentities($_POST['uname'], ENT_QUOTES);

PHP jQuery.indb 193

26.10.2010 14:13:11

194

II. PHP
$pword = htmlentities($_POST['pword'], ENT_QUOTES);
/*
* ,
*
*/
$sql = "SELECT
`user_id`, `user_name`, `user_email`, `user_pass`
FROM `users`
WHERE
`user_name` = :uname
LIMIT 1";
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':uname', $uname, PDO::PARAM_STR);
$stmt->execute();
$user = array_shift($stmt->fetchAll());
$stmt->closeCursor();
}
catch ( Exception $e )
{
die ( $e->getMessage() );
}
/*
* ,
*
*/
if ( !isset($user) )
{
return " .";
}
/*
* - ,
*/
$hash = $this->_getSaltedHash($pword, $user['user_pass']);
/*
* ,
* -
*/
if ( $user['user_pass']==$hash )
{
/*
*
*
*/
$_SESSION['user'] = array(
'id' => $user['user_id'],
'name' => $user['user_name'],
'email' => $user['user_email']
);

PHP jQuery.indb 194

26.10.2010 14:13:11

6.

195

return TRUE;
}
/*
*
*/
else
{
return " .";
}
}

,
. , _getSaltedHash().

-
- , , , , (- , , MD5 SHA1).
. :
http://ru.wikipedia.org/wiki/_-


PHP , (.. ) ,
, , .
(salt), , - .

, SHA1 MD5,
(rainbow tables)1, . , , -,
, , , .
MD5 SHA1, , , , , .
-
,
. ,
, .
:
$hash = sha1($password);
, , , .
1

http://ru.wikipedia.org/wiki/_.

PHP jQuery.indb 195

26.10.2010 14:13:11

196

II. PHP

$salt = substr(md5(time()), 0, 7); //


$hash = $salt . sha1($salt . $password);
.
. ,
.
, -,
. - , .
, , , , -
, .
$salt = substr($dbhash, 0, 7); //
// -
$hash = $salt . sha1($salt . $_POST['password']);
if ( $dbhash==$hash )
{
echo "!";
}
else
{
echo " .";
-
, , .
, .
,
, .
( , , )
.

.
, . , DOS (Denial of Service ),
.. ,
.

, .
1. , . ,
UNIX, ,
$_saltLength,
$salt.
2. , - ,
, ,
$_saltLength, $salt.
3. - .
Admin .

PHP jQuery.indb 196

26.10.2010 14:13:11

6.

197

<?php
class Admin extends DB_Connect
{
private $_saltLength = 7;
public function __construct($db=NULL, $saltLength=NULL) {...}
public function processLoginForm() {...}
/**
* -
*
* @param string $string:
* @param string $salt:
* @return string: -
*/
private function _getSaltedHash($string, $salt=NULL)
{
/*
* ,
*/
if ( $salt==NULL )
{
$salt = substr(md5(time()), 0, $this->_saltLength);
}
/*
* ,
*/
else
{
$salt = substr($salt, 0, $this->_saltLength);
}
/*
* -
*/
return $salt . sha1($salt . $string);
}
}
?>

-
, , _getSaltedHash() testtSaltedHash(). ,
, ,
.
Admin testtSaltedHash().

PHP jQuery.indb 197

26.10.2010 14:13:12

198

II. PHP

<?php
class Admin extends DB_Connect
{
private $_saltLength = 7;
public function __construct($db=NULL, $saltLength=NULL) {...}
public function processLoginForm() {...}
private function _getSaltedHash($string, $salt=NULL) {...}
public function testSaltedHash($string, $salt=NULL)
{
return $this->_getSaltedHash($string, $salt);
}
}
?>

test.php,
testSaltedHash, public (/public/test.php).
, Admin
test - - ,
. - . ,
-, -.
.
<?php
//
include_once '../sys/core/init.inc.php';
// Admin
$obj = new Admin($dbo);
// - "test"
$hash1 = $obj->testSaltedHash("test");
echo "{ 1 :<br />", $hash1, "<br /><br />";
// 1
//
sleep(1);
// - "test"
$hash2 = $obj->testSaltedHash("test");
echo " 2 :<br />", $hash2, "<br /><br />";
// 1
sleep(1);
// "test"
$hash3 = $obj->testSaltedHash("test", $hash2);
echo " 3 2:<br />", $hash3;
?>

PHP jQuery.indb 198

26.10.2010 14:13:12

6.

199

. sleep() , ( ) .

-
- , ,
.
1 :
0286de19e9003b60d7b82686e94e84464eadb2b9b737884
2 :
967ccec3d63bbff88dc263032fe9133d8b7d0f93ea74f75
3 2:
967ccec3d63bbff88dc263032fe9133d8b7d0f93ea74f75

, -
test ,
-. ,
, - , .



,
users _/. testuser, admin,
admin@example.com. ,
; ,
.
, - admin,
testSaltedHash test.php.
- , test.php
, .
<?php
//
include_once '../sys/core/init.inc.php';
// Admin
$obj = new Admin($dbo);
// - "admin"
$pass = $obj->testSaltedHash("admin");
echo '- "admin":<br />', $pass, "<br /><br />";
?>

http://localhost/test.php, .

PHP jQuery.indb 199

26.10.2010 14:13:12

200

II. PHP

- "admin":
8451e0938f510d4aad8975efa57f9f1d72d797dab986ef4

- , http://
localhost/phpmyadmin SQL. testuser , .
INSERT INTO `php-jquery_example`.`users`
(`user_name`, `user_pass`, `user_email`)
VALUES
(
'testuser',
'8451e0938f510d4aad8975efa57f9f1d72d797dab986ef4',
'admin@example.com'
);

, php-jquery_example, users. (Browse) (. 6.3).

. 6.3. testuser

,
- , testSaltedHash()
Admin test.php.




, process.inc.php , .

PHP jQuery.indb 200

26.10.2010 14:13:12

6.

201

, , $actions.
process.inc.php ,
.
<?php
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_edit' => array(
'object' => 'Calendar',
'method' => 'processForm',
'header' => 'Location: ../../'
),
'user_login' => array(
'object' => 'Admin',
'method' => 'processLoginForm',
'header' => 'Location: ../../'
)
);
/*
* , CSRF
*
*/
if ( $_POST['token']==$_SESSION['token']
&& isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
if ( TRUE === $msg=$obj->$use_array['method']() )
{
header($use_array['header']);
exit;
}
else

PHP jQuery.indb 201

26.10.2010 14:13:12

202

II. PHP

{
//
die ( $msg );
}
}
else
{
// /
//
header("Location: ../../");
exit;
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>

.
,
index.php , , ,
.
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/*
* CSS
*/
$page_title = " ";
$css_files = array('style.css', 'admin.css');
/*
*
*/
include_once 'assets/common/header.inc.php';
?>

PHP jQuery.indb 202

26.10.2010 14:13:12

6.

203

<div id="content">
<?php
/*
* HTML
*/
echo $cal->buildCalendar();
?>
</div><!-- end #content -->
<p>
<?php
echo isset($_SESSION['user']) ? " !" :
" !";
?>
</p>
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

http://localhost/, ,
! (. 6.4).
http://localhost/login.php
testuser admin (. 6.5).
,
:
! (. 6.6).



,
. ,
process.inc.php.
_adminGeneralOptions() Calendar.


,
, _adminGeneralOptions
Calendar. , , , process.inc.php user_logout. Calendar

PHP jQuery.indb 203

26.10.2010 14:13:12

204

II. PHP

_adminGeneralOptions() , .

. 6.4.
!

. 6.5. ,

PHP jQuery.indb 204

26.10.2010 14:13:12

6.

205

. 6.6.
!
private function _adminGeneralOptions()
{
/*
*
*/
return <<<ADMIN_OPTIONS
<a href="admin.php" class="admin">+ </a>
<form action="assets/inc/process.inc.php" method="post">
<div>
<input type="submit" value="" class="admin" />
<input type="hidden" name="token"
value="$_SESSION[token]" />
<input type="hidden" name="action"
value="user_logout" />
</div>
</form>
ADMIN_OPTIONS;
}

PHP jQuery.indb 205

26.10.2010 14:13:12

206

II. PHP

http://localhost/, ,
(. 6.7).

. 6.7. Calendar


, Admin
processLogout().
, (user_logout), session_destroy() .
Admin, , .
<?php
class Admin extends DB_Connect
{
private $_saltLength = 7;

PHP jQuery.indb 206

26.10.2010 14:13:12

6.

207

public function __construct($db=NULL, $saltLength=NULL) {...}


public function processLoginForm() {...}
/**
*
*
* @return mixed: TRUE , -*

*/
public function processLogout()
{
/*
* ,
* ACTION
*/
if ( $_POST['action']!='user_logout' )
{
return " processLogout
ACTION.";
}
/*
* user
*/
session_destroy();
return TRUE;
}
private function _getSaltedHash($string, $salt=NULL) {...}
}
?>



, , , $actions
process.inc.php. , .
<?php
/*
*
*/
session_start();
/*
*
*/

PHP jQuery.indb 207

26.10.2010 14:13:12

208

II. PHP

include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_edit' => array(
'object' => 'Calendar',
'method' => 'processForm',
'header' => 'Location: ../../'
),
'user_login' => array(
'object' => 'Admin',
'method' => 'processLoginForm',
'header' => 'Location: ../../'
),
'user_logout' => array(
'object' => 'Admin',
'method' => 'processLogout',
'header' => 'Location: ../../'
)
);
/*
* , CSRF
*
*/
if ( $_POST['token']==$_SESSION['token']
&& isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
if ( TRUE === $msg=$obj->$use_array['method']() )
{
header($use_array['header']);
exit;
}
else
{
//
//
die ( $msg );
}
}
else
{

PHP jQuery.indb 208

26.10.2010 14:13:12

6.

209

// /
//
header("Location: ../../");
exit;
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>

, http://localhost/
. , , : ! (. 6.8).

. 6.8.

PHP jQuery.indb 209

26.10.2010 14:13:12

210

II. PHP

. , , , index.
php , ! !,
<p>.

, , , , , , , .. , .

, , , .

_adminGeneralOptions() _adminEntryOptions() Calendar.

_adminGeneralOptions()
. , , .
, ,
, . _adminGeneralOptions() Calendar , .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayForm() {...}

PHP jQuery.indb 210

26.10.2010 14:13:12

6.

211

public function processForm() {...}


public function confirmDelete($id) {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
private function _adminGeneralOptions()
{
/*
* ,
*
*/
if ( isset($_SESSION['user']) )
{
return <<<ADMIN_OPTIONS
<a href="admin.php" class="admin">+ </a>
<form action="assets/inc/process.inc.php" method="post">
<div>
<input type="submit" value="" class="admin" />
<input type="hidden" name="token"
value="$_SESSION[token]" />
<input type="hidden" name="action"
value="user_logout" />
</div>
</form>
ADMIN_OPTIONS;
}
else
{
return <<<ADMIN_OPTIONS
<a href="login.php">Log In</a>
ADMIN_OPTIONS;
}
}
private function _adminEntryOptions($id) {...}
}
?>

http://localhost/ ,
. ,
(. 6.9).

PHP jQuery.indb 211

26.10.2010 14:13:12

212

II. PHP

. 6.9. ,

_adminEventOptions()
, .
_adminEventOptions() Calendar, .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}

PHP jQuery.indb 212

26.10.2010 14:13:12

6.

213

public function buildCalendar() {...}


public function displayForm() {...}
public function processForm() {...}
public function confirmDelete($id) {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
private function _adminGeneralOptions() {...}
private function _adminEntryOptions($id)
{
if ( isset($_SESSION['user']) )
{
return <<<ADMIN_OPTIONS
<div class="admin-options">
<form action="admin.php" method="post">
<p>
<input type="submit" name="edit_event"
value=" " />
<input type="hidden" name="event_id"
value="$id" />
</p>
</form>
<form action="confirmdelete.php" method="post">
<p>
<input type="submit" name="delete_event"
value=" " />
<input type="hidden" name="event_id"
value="$id" />
</p>
</form>
</div><!-- end .admin-options -->
ADMIN_OPTIONS;
}
else
{
return NULL;
}
}
}
?>

PHP jQuery.indb 213

26.10.2010 14:13:12

214

II. PHP

http://localhost/ ,
.
, , (. 6.10).

. 6.10. ,



,
, , ,
.


, , . , , , , .
admin.php , .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*
*/
if ( !isset($_SESSION['user']) )
{

PHP jQuery.indb 214

26.10.2010 14:13:12

6.

215

header("Location: ./");
exit;
}
/*
*
*/
$page_title = "/ ";
$css_files = array("style.css", "admin.css");
include_once 'assets/common/header.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
?>
<div id="content">
<?php echo $cal->displayForm(); ?>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

, http://localhost/
admin.php , . http://localhost/.



, ,
confirmdelete.php , .
<?php
/*
*
*/
session_start();
/*
* , ID
*/
if ( isset($_POST['event_id']) && isset($_SESSION['user']) )

PHP jQuery.indb 215

26.10.2010 14:13:12

216

II. PHP

{
/*
* ID URL
*/
$id = (int) $_POST['event_id'];
}
else
{
/*
* ID
* ,
*/
header("Location: ./");
exit;
}
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo);
$markup = $cal->confirmDelete($id);
/*
*
*/
$page_title = " ";
$css_files = array("style.css", "admin.css");
include_once 'assets/common/header.inc.php';
?>
<div id="content">
<?php echo $markup; ?>
</div><!-- end #content -->
<?php
/*
*
*/
include_once 'assets/common/footer.inc.php';
?>

http://localhost/
confirmdelete.php , . ,
http://localhost/.

PHP jQuery.indb 216

26.10.2010 14:13:12

6.

217

, , .. ,
, . (Admin),
, .
jQuery ,
.

PHP jQuery.indb 217

26.10.2010 14:13:12

PHP jQuery.indb 218

26.10.2010 14:13:12

III

jQuery
PHP-

, , jQuery . AJAX, JavaScript.

PHP jQuery.indb 219

26.10.2010 14:13:13

PHP jQuery.indb 220

26.10.2010 14:13:13


jQuery

.
, , .


AJAX
.


jQuery
(progressive enhancement) , (Steven Champeon)1 -, ,
- HTML ,
(, CSS- JavaScript)2.
,
.
 ,
, HTML-.
 .

http://www.hesketh.com/about-us/leadership-team.
""
(progressive enhancement) (graceful
degradation).
, , . . .
1
2

PHP jQuery.indb 221

26.10.2010 14:13:13

222

III. jQuery PHP-

 , ..
(, ),
.
 CSS, .
 JavaScript, , JavaScript 3
.
( ,
).
, JavaScript
,
.


,
,
.. , ,
. JavaScript -.
. , AJAX.

jQuery
,
jQuery, .
JavaScript HTML- (</body>),
jQuery , ,
footer.inc.php (/public/assets/common/footer.inc.php). jQuery ; footer.
inc.php , .
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
</body>
</html>

3
JavaScript (unobtrusive JavaScript)
- (., , http://ru.wikipedia.
org/wiki/_JavaScript). . .

PHP jQuery.indb 222

26.10.2010 14:13:13

7. jQuery

223

http://localhost. Firebug , jQuery .


$("h2").text();

.
>>> $("h2").text();
"January 2010"
. Google JSAPI, Apache
. ,
jQuery http://jquery.com .

JavaScript
,
init.js. js (/public/assets/js/init.js)
jQuery .


, .
jQuery, ,
jQuery footer.inc.php.
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript"
src="assets/js/init.js"></script>
</body>
</html>


init.js, , JavaScript
,
. jQuery
$(document).ready() , $ jQuery() ,
. init.js .
// ,
jQuery(function($){
// ,
console.log(" init.js .");
});

PHP jQuery.indb 223

26.10.2010 14:13:13

224

III. jQuery PHP-

http://localhost/
Firebug. , .
init.js .

,
jQuery
jQuery
, CSS-, ,
jQuery.
ajax.css css (/public/assets/css/
ajax.css). , .
.modal-overlay {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,.5);
z-index: 4;
}
.modal-window {
position: absolute;
top: 140px;
left: 50%;
width: 300px;
height: auto;
margin-left: -150px;
padding: 20px;
border: 2px solid #000;
background-color: #FFF;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: 0 0 14px #123;
-webkit-box-shadow: 0 0 14px #123;
box-shadow: 0 0 14px #123;
z-index: 5;
}
.modal-close-btn {
position: absolute;
top: 0;
right: 4px;
margin: 0;
padding: 0;
text-decoration: none;
color: black;

PHP jQuery.indb 224

26.10.2010 14:13:13

7. jQuery

225

font-size: 16px;
}
.modal-close-btn:before {
position: relative;
top: -1px;
content: "Close";
text-transform: uppercase;
font-size: 10px;
}

index.php
index.php $css_files,
, .
<?php
/*
*
*/
include_once '../sys/core/init.inc.php';
/*
*
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/*
* CSS
*/
$page_title = " ";
$css_files = array('style.css', 'admin.css', 'ajax.css');
/*
*
*/
include_once 'assets/common/header.inc.php';
?>
<div id="content">
<?php
/*
* HTML
*/
echo $cal->buildCalendar();
?>
</div><!-- end #content -->
<?php
/*

PHP jQuery.indb 225

26.10.2010 14:13:13

226

III. jQuery PHP-

*
*/
include_once 'assets/common/footer.inc.php';
?>



.
:
 , ( view.php, );
 active ;
 href ;
 , ;
 ;
 AJAX .
click, .

, init.js ,
<a>, (li>a), live() click.
init.js , .
// ,
jQuery(function($){
//
$("li>a").live("click", function(event){
//
});
});


active
,
, .preventDefault(), active , ,
.addClass().

PHP jQuery.indb 226

26.10.2010 14:13:13

7. jQuery

227

init.js ,
.
// ,
jQuery(function($){
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// , ,
//
console.log( $(this).text() );
});
});

, http://localhost/
. view.php ; . , New Years Day,
.
New Year's Day

,
- , .
, href .
. ( href http://localhost/view.php?event_id=1, event_id=1.)
:
Java Script .replace() . .replace() , ,
, .

:
:
var data = string.replace("http://localhost/view.php?", "");

, "event_id=1" ( , $string http://localhost/view.


php?event_id=1). , . -

PHP jQuery.indb 227

26.10.2010 14:13:13

228

III. jQuery PHP-


event.php?
.

:
: . ,
.
, , (?) , , .
:
/.*?\?(.*)$/

JavaScript (/), . ( ) , , ,
,
.
.
9.


href , , this. jQuery
jQuery.
href .attrib(), .replace() .
.replace() , . ,
, init.js , ,
, , data. ,
.
// ,
jQuery(function($){
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// "href"
var data = $(this)

PHP jQuery.indb 228

26.10.2010 14:13:13

7. jQuery

229

.attr("href")
.replace(/.+?\?(.*)$/, "$1");
//
console.log( data );
});
});

, http://localhost/ . .
event_id=1


HTML-,
. div, . , New Years Day .
<div class="modal-window">
<h2>New Year's Day</h2>
<p class="dates">January 01, 2010, 12:00am11:59pm</p>
<p>Happy New Year!</p>
</div>

(,
, ),
, . ,
, , , -, ( ).



init.js fx,
.
// ,
jQuery(function($){
//
var fx = {};
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"

PHP jQuery.indb 229

26.10.2010 14:13:13

230

III. jQuery PHP-


$(this).addClass("active");
// "href"
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1");
//
console.log( data );

});
});

, fx,
initModal. , . , ,
body.
,
length jQuery . length 0, ,
(DOM).
,
fx init.js , .
//
var fx = {
// , ;
//
"initModal" : function() {
// ,
// length 0
if ( $(".modal-window").length==0 )
{
// div,
// body
return $("<div>")
.addClass("modal-window")
.appendTo("body");
}
else
{
// , DOM
return $(".modal-window");
}
}
};


click fx.initModal , ,
init.js , .

PHP jQuery.indb 230

26.10.2010 14:13:13

7. jQuery

231

//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// "href"
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1"),
//
//
modal = fx.initModal();
});
. (;), data, (,).

, http://localhost/
(. 7.1).

. 7.1.

PHP jQuery.indb 231

26.10.2010 14:13:13

232

III. jQuery PHP-


. , ,
, .
. .

JavaScript c , .
var obj = {};
, -,
.
var obj = {
"name" : "Jason Lengstorf",
"age" : "25"
};
- , (.)
.
alert(obj.name); // "Jason Lengstorf"
, , .
var obj = {
"func" : function() { alert(" -- !"); }
};
, , , , , .
JavaScript , ,
.
obj.func(); // " -- !"
, .
var obj = {
"func" : function(text){ alert(text); }
};
obj.func(" !"); // " !"

, , , ,
,
.
, . , ,
- . JavaScript , .
, , , ,
. ;
, .

PHP jQuery.indb 232

26.10.2010 14:13:13

7. jQuery

233


AJAX
, ,
. $.ajax().
$.ajax(), (
), POST,
.

AJAX

$.ajax(), , .
inc ajax.inc.php (public/assets/inc/ajax.
inc.php). , process.
inc.php, , AJAX. , PHP-,
JavaScript , ( echo ), process.inc.php .
, ajax.inc.php , , ,
echo AJAX.
, , , , . ajax.inc.php .
<?php
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{

PHP jQuery.indb 233

26.10.2010 14:13:13

234

III. jQuery PHP-


include_once $filename;

}
}
?>

,
, ,
, , .
<?php
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_view' => array(
'object' => 'Calendar',
'method' => 'displayEvent'
)
);
/*
* , CSRF
*
*/
if ( isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
/*
* ID
*/
if ( isset($_POST['event_id']) )
{

PHP jQuery.indb 234

26.10.2010 14:13:13

7. jQuery

235

$id = (int) $_POST['event_id'];


}
else { $id = NULL; }
echo $obj->$use_array['method']($id);
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>


process.inc.php header echo , .

AJAX
init.js, $.ajax(). $.ajax(),
, , .
init.js, ,
.
// ,
//
jQuery(function($){
// , AJAX
var processFile = "assets/inc/ajax.inc.php",
//
fx = {
// , ;
//
"initModal" : function() {
// ,
// length 0
if ( $(".modal-window").length==0 )
{
// div,
// body
return $("<div>")
.addClass("modal-window")
.appendTo("body");

PHP jQuery.indb 235

26.10.2010 14:13:13

236

III. jQuery PHP-


}
else
{
// , DOM
return $(".modal-window");
}
}

};
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// "href"
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1"),
//
//
modal = fx.initModal();
});
});

$.ajax() .
POST, processFile . , , action, . , .append()
, , .
init.js , .
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// "href"
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1"),
//
//

PHP jQuery.indb 236

26.10.2010 14:13:13

7. jQuery

237

modal = fx.initModal();
//
$.ajax({
type: "POST",
url: processFile,
data: "action=event_view&" + data,
success: function(data){
//
modal.append(data);
},
error: function(msg) {
modal.append(msg);
}
});
});

, http://localhost/
,
(. 7.2).

. 7.2.

PHP jQuery.indb 237

26.10.2010 14:13:13

238

III. jQuery PHP-


, , , , . , ,
, .
, DOM. , ( , ajax.
css, ). , href,
,
, .
, init.js , .
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// "href"
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1"),
//
//
modal = fx.initModal();
//
$("<a>")
.attr("href", "#")
.addClass("modal-close-btn")
.html("&times;")
.click(function(event){
//
event.preventDefault();
//
$(".modal-window")
.remove();
})
.appendTo(modal);
//
$.ajax({
type: "POST",
url: processFile,
data: "action=event_view&" + data,
success: function(data){

PHP jQuery.indb 238

26.10.2010 14:13:13

7. jQuery

239

//
modal.append(data);
},
error: function(msg) {
modal.append(msg);
}
});
});

, http://localhost/
, (. 7.3).
.

. 7.3.



, ,
. ,
, , ,
.

PHP jQuery.indb 239

26.10.2010 14:13:13

240

III. jQuery PHP-


, , ( ).
,
. , , , , , , .
active ,
, .
, .fadeOut(). DOM.
, fx ,
.
//
fx = {
// , ;
//
"initModal" : function() {
// ,
// length 0
if ( $(".modal-window").length==0 )
{
// div,
// body
return $("<div>")
.addClass("modal-window")
.appendTo("body");
}
else
{
// , DOM
return $(".modal-window");
}
},
// DOM
"boxout" : function(event) {
// ,
// ,
// ,
if ( event!=undefined )
{
event.preventDefault();
}
// "active"
$("a").removeClass("active");
// ,
// DOM
$(".modal-window")
.fadeOut("slow", function() {
$(this).remove();

PHP jQuery.indb 240

26.10.2010 14:13:13

7. jQuery

241

}
);
}
};

, , ,
.
//
$("<a>")
.attr("href", "#")
.addClass("modal-close-btn")
.html("&times;")
.click(function(event){
//
fx.boxout(event);
})
.appendTo(modal);

init.js http://localhost/ .
, , , (. 7.4).

. 7.4.

PHP jQuery.indb 241

26.10.2010 14:13:13

242

III. jQuery PHP-


, fx , boxin. $.ajax()
: ,
ajax.inc.php (data), (modal).
div modal-overlay,
div .

, fx.boxout()
.
, data. , .fadeIn().
fx, ,
.
//
fx = {
// , ;
//
"initModal" : function() {
// ,
// length 0
if ( $(".modal-window").length==0 )
{
// div,
// body
return $("<div>")
.addClass("modal-window")
.appendTo("body");
}
else
{
// , DOM
return $(".modal-window");
}
},
//
"boxin" : function(data, modal) {
// ,
//
$("<div>")
.hide()
.addClass("modal-overlay")
.click(function(event){
//
fx.boxout(event);
})
.appendTo("body");

PHP jQuery.indb 242

26.10.2010 14:13:13

7. jQuery

243

//
//
modal
.hide()
.append(data)
.appendTo("body");
//
$(".modal-window,.modal-overlay")
.fadeIn("slow");
},
// DOM
"boxout" : function(event) {
// ,
// ,
// ,
if ( event!=undefined )
{
event.preventDefault();
}
// "active"
$("a").removeClass("active");
// ,
// DOM
$(".modal-window")
$(".modal-window,.modal-overlay")
.fadeOut("slow", function() {
$(this).remove();
}
);
}
};

,
$.ajax() , fxboxin. ,
.
//
$("li>a").live("click", function(event){
// view.php
event.preventDefault();
// "active"
$(this).addClass("active");
// href
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1"),

PHP jQuery.indb 243

26.10.2010 14:13:14

244

III. jQuery PHP-


//
//
modal = fx.initModal();
//
$("<a>")
.attr("href", "#")
.addClass("modal-close-btn")
.html("&times;")
.click(function(event){
//
fx.boxout(event);
})
.appendTo(modal);
//
$.ajax({
type: "POST",
url: processFile,
data: "action=event_view&" + data,
success: function(data){
fx.boxin(data, modal);
},
error: function(msg) {
modal.append(msg);
}
});

});

, http://localhost/
, (. 7.5).
, ,
. , fx.initModal()
.
, .hide()
fx.initModal() , .
//
fx = {
// , ;
//
"initModal" : function() {
// ,
// length 0
if ( $(".modal-window").length==0 )
{
// div,
// body
return $("<div>")
.hide()
.addClass("modal-window")
.appendTo("body");

PHP jQuery.indb 244

26.10.2010 14:13:14

7. jQuery

245

}
else
{
// , DOM
return $(".modal-window");
}
},
//
"boxin" : function(data, modal) {
//
},
// DOM
"boxout" : function(event) {
//
}
};

. 7.5.

, , .
fx.boxout().

PHP jQuery.indb 245

26.10.2010 14:13:14

246

III. jQuery PHP-

//
fx = {
// , ;
//
"initModal" : function() {
//
},
//
//
"boxin" : function(data, modal) {
//
},
// DOM
"boxout" : function(event) {
// ,
// ,
// ,
if ( event!=undefined )
{
event.preventDefault();
}
// active
$("a").removeClass("active");
//
// , DOM
$(".modal-window,.modal-overlay")
.fadeOut("slow", function() {
$(this).remove();
}
);
}
};

, http://localhost/
. ,
.


jQuery, . , .
AJAX
, AJAX
, .

PHP jQuery.indb 246

26.10.2010 14:13:14


AJAX jQuery

,
, ,
AJAX -. , - ,
, ,
, . , , , ,
.
, , AJAX
, ,
. -
, .
, ,
,
. , , ,
.
. , , . testuser,
admin.


, , . init.
js ,
(admin)1. click,
1
, admin,
.
, admin (. 6)
function(event) .
. .

PHP jQuery.indb 247

26.10.2010 14:13:14

248

III. jQuery PHP-

, , ( ) , , .
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...}
};
$("li>a").live("click", function(event){...});
//
$(".admin").live("click", function(event){
//
event.preventDefault();
//
console.log( " !" );
});
});
. , ,
, . -
Apress http://apress.com/book/view/1430228474.

http://localhost/.
.
!

AJAX
action , . ,
, event_edit.
$.ajax().
;
.

form.
fx.initModal() fx.boxin(), null.
, , edit-form,
.

PHP jQuery.indb 248

26.10.2010 14:13:14

8. AJAX jQuery

249

init.js , .
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...}
};
$("li>a").live("click", function(event){...});
//
$(".admin").live("click", function(event){
//
event.preventDefault();
// action
var action = "edit_event";
//
$.ajax({
type: "POST",
url: processFile,
data: "action="+action,
success: function(data){
//
var form = $(data).hide(),
//
modal = fx.initModal();
// boxin
//
fx.boxin(null, modal);
// ,
//
form
.appendTo(modal)
.addClass("edit-form")
.fadeIn("slow");
},
error: function(msg){
alert(msg);
}
});
});
});

PHP jQuery.indb 249

26.10.2010 14:13:14

250

III. jQuery PHP-

AJAX

AJAX ,
ajax.inc.php. , Calendar,
displayForm(), , .
<?php
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_view' => array(
'object' => 'Calendar',
'method' => 'displayEvent'
),
'edit_event' => array(
'object' => 'Calendar',
'method' => 'displayForm'
)
);
/*
* , CSRF
*
*/
if ( isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
/*
* ID
*/
if ( isset($_POST['event_id']) )

PHP jQuery.indb 250

26.10.2010 14:13:14

8. AJAX jQuery

251

{
$id = (int) $_POST['event_id'];
}
else { $id = NULL; }
echo $obj->$use_array['method']($id);
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>

, http://localhost/ .
(. 8.1).

. 8.1. ,

PHP jQuery.indb 251

26.10.2010 14:13:14

252

III. jQuery PHP-


, . ,
, .
, ,
fx.boxout(),
.
, .live()
click , , edit-form.
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...}
};
$("li>a").live("click", function(event){...});
$(".admin").live("click", function(event){...});
// ""
// ""
//
$(".edit-form a:contains()").live("click", function(event){
fx.boxout(event);
});
});

, http://localhost/ . ,
. ,
, .


, , , click.
serialize() .
ajax.inc.php POST.
click
submit , edit-form. .live()
, . ,
, event.PreventDefault().

PHP jQuery.indb 252

26.10.2010 14:13:14

8. AJAX jQuery

253

init.js
, .
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...}
};
$("li>a").live("click", function(event){...});
$(".admin").live("click", function(event){...});
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
//
console.log( " !" );
});
$(".edit-form a:contains()")
.live("click", function(event){...};
});

.
,
. .
!


,
. , jQuery .serialize(). , -, (&).
init.js , , ,
, . , .
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();

PHP jQuery.indb 253

26.10.2010 14:13:14

254

III. jQuery PHP-


//
// $.ajax()
var formData = $(this).parents("form").serialize();
//
console.log( formData );

});

.
:
 Test Event;
 2010-01-04 08:00:00;
 2010-01-04 10:00:00;
 This is a test description.
. ( ).
event_title=Test+Event&event_start=2010-01-04+08%3A00%3A00
&event_end=2010-01-04+10%3A00%3A00&event_description=This+
is+a+test+description&event_id=&token=21697f4a2326d41995c1
d6291cd5637ca1bbcfbe&action=event_edit



$.ajax().
ajax.inc.php POST,

fx.boxout() .
, .
,
.
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
// $.ajax()
var formData = $(this).parents("form").serialize();
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
//
fx.boxout();

PHP jQuery.indb 254

26.10.2010 14:13:15

8. AJAX jQuery

255

//
console.log( " !" );
},
error: function(msg) {
alert(msg);
}
});
});

.
ajax.inc.php .


AJAX
ajax.inc.php ,
,
.
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_view' => array(
'object' => 'Calendar',
'method' => 'displayEvent'
),
'edit_event' => array(
'object' => 'Calendar',
'method' => 'displayForm'
),
'event_edit' => array(
'object' => 'Calendar',
'method' => 'processForm'
)
);

PHP jQuery.indb 255

26.10.2010 14:13:15

256

III. jQuery PHP-

/*
* , CSRF
*
*/
if ( isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
/*
* ID
*
*/
if ( isset($_POST['event_id']) )
{
$id = (int) $_POST['event_id'];
}
else { $id = NULL; }
echo $obj->$use_array['method']($id);
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}
?>

http://localhost/.
, :
 Test Event;
 2010-01-04 08:00:00;
 2010-01-04 10:00:00;
 This is a test description.
. , .
!

: , . ,
,

.

PHP jQuery.indb 256

26.10.2010 14:13:15

8. AJAX jQuery

257



. , ,
:
 ;
 , ;
 , ;
 ;
 , ;

.
fx, addevent , ajax.inc.php (data),
(formData).
fx, ini.js
, .
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
}
};
$("li>a").live("click", function(event){...});
$(".admin").live("click", function(event){...});
$(".edit-form input[type=submit]")
.live("click", function(event){...});
$(".edit-form a:contains()")
.live("click", function(event){...};
});


, , . -

PHP jQuery.indb 257

26.10.2010 14:13:15

258

III. jQuery PHP-

, fx
deserialize, (str).
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
},
//
//
"deserialize" : function(str){
//
}
};

, -, (=),
(&). ,
:
name1=value1&name2=value2

JavaScript .split().
-,
.
Array
(
0 => "name1=value1",
1 => "name2=value2"
)

.
pairs. , - ,
, .
.
Array
(
0 => "name1",
1 => "value1"
)

key val ,
entry.
data.
deserialize.
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},

PHP jQuery.indb 258

26.10.2010 14:13:15

8. AJAX jQuery

259

//
"addevent" : function(data, formData){
//
},
//
//
"deserialize" : function(str){
// -
var data = str.split("&"),
//
pairs=[], entry={}, key, val;
// -
for ( x in data )
{
//
pairs = data[x].split("=");
// --
key = pairs[0];
// --
val = pairs[1];
//
//
entry[key] = val;
}
return entry;
}
};

,
URL
fx.deserialize , , , URL. , ,
. , Im testing & logging .
I'm+testing+%26+logging!

, (+)
/\+/g;
+. g, ,
, , , .
JavaScript decodeURIComponent().
fx urldecode .

PHP jQuery.indb 259

26.10.2010 14:13:15

260

III. jQuery PHP-

fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
},
//
"deserialize" : function(str){
// "-"
var data = str.split("&"),
//
pairs=[], entry={}, key, val;
// "-"
for ( x in data )
{
//
pairs = data[x].split("=");
// --
key = pairs[0];
// --
val = pairs[1];
//
//
entry[key] = val;
}
return entry;
},
//
"urldecode" : function(str) {
// +
var converted = str.replace(/\+/g, ' ');
//
//
return decodeURIComponent(converted);
}
};

fx.urldecode fx.desrialize, , .
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//

PHP jQuery.indb 260

26.10.2010 14:13:15

8. AJAX jQuery

261

"addevent" : function(data, formData){


//
},
//
"deserialize" : function(str){
// "-"
var data = str.split("&"),
//
pairs=[], entry={}, key, val;
// "-"
for ( x in data )
{
//
pairs = data[x].split("=");
// --
key = pairs[0];
// --
val = pairs[1];
// URL-
//
entry[key] = fx.urldecode(val);
}
return entry;
},
"urldecode" : function(str) {...}
};


fx.deserialize fx.urldecode, fx.addevent, (entry)
.
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
var entry = fx.deserialize(formData);
},
"deserialize" : function(str){...},
"urldecode" : function(str...}
};

PHP jQuery.indb 261

26.10.2010 14:13:15

262

III. jQuery PHP-

Date
, , , , .
. Date JavaScript, , .
Date, - :
http://w3schools.com/jsref/jsref_obj_date.asp

Calendar
Date ,
(ID) h2, . buildCalendar()
Calendar , .
public function buildCalendar()
{
/*
*
* ,
*
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$cal_id = date('Y-m', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue',
'Wed', 'Thu', 'Fri', 'Sat');
/*
* HTML-
*/
$html = "\n\t<h2 id=\"month-$cal_id\">$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
}
$html .= "\n\t<ul class=\"weekdays\">"
. $labels . "\n\t</ul>";
//
)
. month , W3, .

Date JavaScript
, ,
Date: .
Date, ID h2 attr(), ,
, cdata.

PHP jQuery.indb 262

26.10.2010 14:13:15

8. AJAX jQuery

263

entry.event_start
, , ( --) date.
, , edata.
Date, cdata edata
cal event .
, fx.addevent , .
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
var entry = fx.deserialize(formData),
// "date"
cal = new Date(NaN),
// "date"
event = new Date(NaN),
// ID H2
cdata = $("h2").attr("id").split('-'),
// ,
date = entry.event_start.split(' ')[0],
//
edata = date.split('-');
// "date"
cal.setFullYear(cdata[1], cdata[2], 1);
// "date"
event.setFullYear(edata[0], edata[1], edata[2]);
},
"deserialize" : function(str){...},
"urldecode" : function(str) {...}
};


Date,
(00:00:00 GMT). , , ,
. ,
, Date:
.setMinutes() .getTimezoneOffset().

PHP jQuery.indb 263

26.10.2010 14:13:15

264

III. jQuery PHP-

.getTimezoneOffset() GMT . , MST2 (-0700) .getTimezoneOffset() 420.


.setMinutes(),
Date, , , .
, .
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
var entry = fx.deserialize(formData),
// "date"
cal = new Date(NaN),
// "date"
event = new Date(NaN),
// ID H2
cdata = $("h2").attr("id").split('-'),
// ,
date = entry.event_start.split(' ')[0],
//
edata = date.split('-');
// "date"
cal.setFullYear(cdata[1], cdata[2], 1);
// "date"
event.setFullYear(edata[0], edata[1], edata[2]);
// "date" GMT,
// ,
// ,
event.setMinutes(event.getTimezoneOffset());
},
"deserialize" : function(str){...},
"urldecode" : function(str) {...}
};

2
Mountain Standard Time (-7 ,
). . .

PHP jQuery.indb 264

26.10.2010 14:13:15

8. AJAX jQuery

265

,
, , , . , ,
, getDay() Date. ,
, , String().
, .
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
var entry = fx.deserialize(formData),
// "date"
cal = new Date(NaN),
// "date"
event = new Date(NaN),
// ID H2
cdata = $("h2").attr("id").split('-'),
// ,
date = entry.event_start.split(' ')[0],
//
edata = date.split('-');
// "date"
cal.setFullYear(cdata[1], cdata[2], 1);
// "date"
event.setFullYear(edata[0], edata[1], edata[2]);
// "date" GMT,
// ,
// ,
event.setMinutes(event.getTimezoneOffset());
// , ,
//
if ( cal.getFullYear()==event.getFullYear()
&& cal.getMonth()==event.getMonth() )
{
//
var day = String(event.getDate());

PHP jQuery.indb 265

26.10.2010 14:13:15

266

III. jQuery PHP-


// ,
//
day = day.length==1 ? "0"+day : day;

}
},
"deserialize" : function(str){...},
"urldecode" : function(str) {...}
};


- , .
<a>, , href .
.delay(1000) .
,
.
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
//
"addevent" : function(data, formData){
//
var entry = fx.deserialize(formData),
// "date"
cal = new Date(NaN),
// "date"
event = new Date(NaN),
// ID H2
cdata = $("h2").attr("id").split('-'),
// ,
date = entry.event_start.split(' ')[0],
//
edata = date.split('-');
// "date"
cal.setFullYear(cdata[1], cdata[2], 1);
// "date"
event.setFullYear(edata[0], edata[1], edata[2]);
// "date" GMT,
// ,
// ,
event.setMinutes(event.getTimezoneOffset());

PHP jQuery.indb 266

26.10.2010 14:13:15

8. AJAX jQuery

267

// , ,
//
if ( cal.getFullYear()==event.getFullYear()
&& cal.getMonth()==event.getMonth() )
{
//
var day = String(event.getDate());
// ,
//
day = day.length==1 ? "0"+day : day;
//
$("<a>")
.hide()
.attr("href", "view.php?event_id="+data)
.text(entry.event_title)
.insertAfter($("strong:contains("+day+")"))
.delay(1000)
.fadeIn("slow");
}
},
"deserialize" : function(str){...},
"urldecode" : function(str) {...}
};
. data . .

click success $.ajax()


fx.addevent() , .
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
//
// $.ajax()
var formData = $(this).parents("form").serialize();
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
//
fx.boxout();
//
fx.addevent(data, formData);
},

PHP jQuery.indb 267

26.10.2010 14:13:15

268

III. jQuery PHP-


error: function(msg) {
alert(msg);
}
});

});

http://localhost/.
, :
 Addition Test;
 2010-01-09 12:00:00;
 2010-01-09 14:00:00;
 This is a test of the dynamic addition of new events
to the calendar.
;

(. 8.2).

. 8.2.

PHP jQuery.indb 268

26.10.2010 14:13:15

8. AJAX jQuery

269


;
. ( ),
(. 8.3).

. 8.3.


, Calendar.
(/sys/class/class.calendar.inc.php) processForm().
return , (ID) , PDO
lastInserttId().
public function processForm()
{
/*
* , "action"
*/

PHP jQuery.indb 269

26.10.2010 14:13:15

270

III. jQuery PHP-


if ( $_POST['action']!='event_edit' )
{
return " processForm";
}
/*
*
*/
$title = htmlentities($_POST['event_title'], ENT_QUOTES);
$desc = htmlentities($_POST['event_description'], ENT_QUOTES);
$start = htmlentities($_POST['event_start'], ENT_QUOTES);
$end = htmlentities($_POST['event_end'], ENT_QUOTES);
/*
* ID ,
*/
if ( empty($_POST['event_id']) )
{
$sql = "INSERT INTO `events`
(`event_title`, `event_desc`, `event_start`,
`event_end`)
VALUES
(:title, :description, :start, :end)";
}
/*
* ,
*/
else
{
/*
* ID
*
*/
$id = (int) $_POST['event_id'];
$sql = "UPDATE `events`
SET
`event_title`=:title,
`event_desc`=:description,
`event_start`=:start,
`event_end`=:end
WHERE `event_id`=$id";
}
/*
*
*
*/
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(":title", $title, PDO::PARAM_STR);
$stmt->bindParam(":description", $desc, PDO::PARAM_STR);
$stmt->bindParam(":start", $start, PDO::PARAM_STR);
$stmt->bindParam(":end", $end, PDO::PARAM_STR);

PHP jQuery.indb 270

26.10.2010 14:13:15

8. AJAX jQuery

271

$stmt->execute();
$stmt->closeCursor();
/*
* ID
*/
return $this->db->lastInsertId();
}
catch ( Exception $e )
{
return $e->getMessage();
}
}

, http://
localhost/ . ,
:
 ID Test;
 2010-01-06 12:00:00;
 2010-01-06 16:00:00;
 This event should be immediately viewable after
creation.
. , (. 8.4).


,
, . click, , .
,
admin. , .
//
$(".admin-options form,.admin").live("click", function(event){
//
event.preventDefault();
// "action"
var action = "edit_event";
//
$.ajax({
type: "POST",
url: processFile,
data: "action="+action,
success: function(data){
//
var form = $(data).hide(),

PHP jQuery.indb 271

26.10.2010 14:13:16

272

III. jQuery PHP-


//
modal = fx.initModal();
// boxin
//
fx.boxin(null, modal);
// ,
//
form
.appendTo(modal)
.addClass("edit-form")
.fadeIn("slow");
},
error: function(msg){
alert(msg);
}
});

});

. 8.4.

PHP jQuery.indb 272

26.10.2010 14:13:16

8. AJAX jQuery

273

action
,
, action, , (, edit_event delete_event
). ajax.inc.php action .
,
(edit_event).
, , target event. ,
. target event jQuery(), , ,
.attr().
,
, .
//
$(".admin-options form,.admin").live("click", function(event){
//
event.preventDefault();
// "action"
var action = $(event.target).attr("name") || "edit_event";
// "action"
var action = "edit_event";
//
$.ajax({
type: "POST",
url: processFile,
data: "action="+action,
success: function(data){
//
var form = $(data).hide(),
//
modal = fx.initModal();
// boxin
//
//
fx.boxin(null, modal);
// ,
//
//
form
.appendTo(modal)
.addClass("edit-form")

PHP jQuery.indb 273

26.10.2010 14:13:16

274

III. jQuery PHP-


.fadeIn("slow");
},
error: function(msg){
alert(msg);
}
});

});

, , ,
. , event.
target, event_id,
id.
, .
//
$(".admin-options form,.admin").live("click", function(event){
//
event.preventDefault();
// "action"
var action = $(event.target).attr("name") || "edit_event";
// event_id
id = $(event.target)
.siblings("input[name=event_id]")
.val();
// "action"
var action = "edit_event";
//
$.ajax({
type: "POST",
url: processFile,
data: "action="+action,
success: function(data){
//
var form = $(data).hide(),
//
modal = fx.initModal();
// boxin
//
//
fx.boxin(null, modal);

PHP jQuery.indb 274

26.10.2010 14:13:16

8. AJAX jQuery

275

// ,
//
//
form
.appendTo(modal)
.addClass("edit-form")
.fadeIn("slow");
},
error: function(msg){
alert(msg);
}
});
});


id,
ajax.inc.php.
, ,
event_id -. , ,
.
//
$(".admin-options form,.admin").live("click", function(event){
//
event.preventDefault();
// "action"
var action = $(event.target).attr("name") || "edit_event";
// event_id
id = $(event.target)
.siblings("input[name=event_id]")
.val();
// ,
//
id = ( id!=undefined ) ? "&event_id="+id : "";
// "action"
var action = "edit_event";
//
$.ajax({
type: "POST",
url: processFile,
data: "action="+action+id,
success: function(data){
//
var form = $(data).hide(),

PHP jQuery.indb 275

26.10.2010 14:13:16

276

III. jQuery PHP-


//
modal = fx.initModal();
// boxin
//
//
fx.boxin(null, modal);
// ,
//
//
form
.appendTo(modal)
.addClass("edit-form")
.fadeIn("slow");
},
error: function(msg){
alert(msg);
}
});

});


, .
success, fx.iniModal(),
, , .
.end(),
. ( .children() jQuery
, .)
, .
//
$(".admin-options form,.admin").live("click", function(event){
//
event.preventDefault();
// "action"
var action = $(event.target).attr("name") || "edit_event";
// event_id
id = $(event.target)
.siblings("input[name=event_id]")
.val();
// ,
//
id = ( id!=undefined ) ? "&event_id="+id : "";
// "action"
var action = "edit_event";

PHP jQuery.indb 276

26.10.2010 14:13:16

8. AJAX jQuery

277

//
//
$.ajax({
type: "POST",
url: processFile,
data: "action="+action+id,
success: function(data){
//
var form = $(data).hide(),
//
modal = fx.initModal();
.children(":not(.modal-close-btn)")
.remove()
.end();
// boxin
//
//
fx.boxin(null, modal);
// ,
//
//
form
.appendTo(modal)
.addClass("edit-form")
.fadeIn("slow");
},
error: function(msg){
alert(msg);
}
});
});

, http://localhost/ New Years Day, .


. ,

(. 8.5).


New Years Day, , (. 8.6).
, click . (ID) event_id , , .
, fx.addevent() .

PHP jQuery.indb 277

26.10.2010 14:13:16

278

III. jQuery PHP-

. 8.5.

, .
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
// $.ajax()
var formData = $(this).parents("form").serialize();
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
//
fx.boxout();

PHP jQuery.indb 278

26.10.2010 14:13:16

8. AJAX jQuery

279

// ,
//
if ( $("[name=event_id]").val().length==0 )
{
fx.addevent(data, formData);
}
},
error: function(msg) {
alert(msg);
}
});
});

. 8.6.

,
.

PHP jQuery.indb 279

26.10.2010 14:13:16

280

III. jQuery PHP-



, .
, , , .


, , ajax.inc.php , .
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_view' => array(
'object' => 'Calendar',
'method' => 'displayEvent'
),
'edit_event' => array(
'object' => 'Calendar',
'method' => 'displayForm'
),
'event_edit' => array(
'object' => 'Calendar',
'method' => 'processForm'
),
'delete_event' => array(
'object' => 'Calendar',
'method' => 'confirmDelete'

PHP jQuery.indb 280

26.10.2010 14:13:16

8. AJAX jQuery

281

)
);
/*
* , CSRF
*
*/
if ( isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
/*
* ID
*
*/
if ( isset($_POST['event_id']) )
{
$id = (int) $_POST['event_id'];
}
else { $id = NULL; }
echo $obj->$use_array['method']($id);
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}

(. 8.7).

,

init.js. ,
. ,
, , !
!, , , , .
this jQuery, , .val(),
submitVal. , confirm_delete
name . , confirm_delete
.

PHP jQuery.indb 281

26.10.2010 14:13:16

282

III. jQuery PHP-

. 8.7.

, ,
.
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
//
// $.ajax()
var formData = $(this).parents("form").serialize(),
// "submit"
submitVal = $(this).val();
// ,
if ( $(this).attr("name")=="confirm_delete" )
{
//

PHP jQuery.indb 282

26.10.2010 14:13:16

8. AJAX jQuery

283

formData += "&action=confirm_delete"
+ "&confirm_delete="+submitVal;
}
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
//
fx.boxout();
// ,
//
if ( $("[name=event_id]").val().length==0 )
{
fx.addevent(data, formData);
}
},
error: function(msg) {
alert(msg);
}
});
});


, ,
ajax.inc.php .
/*
*
*/
session_start();
/*
*
*/
include_once '../../../sys/config/db-cred.inc.php';
/*
*
*/
foreach ( $C as $name => $val )
{
define($name, $val);
}
/*
* ,
*/
$actions = array(
'event_view' => array(

PHP jQuery.indb 283

26.10.2010 14:13:16

284

III. jQuery PHP-


'object' => 'Calendar',
'method' => 'displayEvent'
),
'edit_event' => array(
'object' => 'Calendar',
'method' => 'displayForm'
),
'event_edit' => array(
'object' => 'Calendar',
'method' => 'processForm'
),
'delete_event' => array(
'object' => 'Calendar',
'method' => 'confirmDelete'
),
'confirm_delete' => array(
'object' => 'Calendar',
'method' => 'confirmDelete'
)

);
/*
* , CSRF
*
*/
if ( isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
/*
* ID
*
*/
if ( isset($_POST['event_id']) )
{
$id = (int) $_POST['event_id'];
}
else { $id = NULL; }
echo $obj->$use_array['method']($id);
}
function __autoload($class_name)
{
$filename = '../../../sys/class/class.'
. strtolower($class_name) . '.inc.php';
if ( file_exists($filename) )
{
include_once $filename;
}
}

PHP jQuery.indb 284

26.10.2010 14:13:16

8. AJAX jQuery

285

, ID Test.
.
, (. 8.8).

. 8.8. ,


, ,
, . , .
fx removeevent.
active, , ,
DOM. , .
fx = {
"initModal" : function() {...},
"boxin" : function(data, modal) {...},
"boxout" : function(event) {...},
"addevent" : function(data, formData) {...},

PHP jQuery.indb 285

26.10.2010 14:13:16

286

III. jQuery PHP-


//
"removeevent" : function()
{
// "active"
$(".active")
.fadeOut("slow", function(){
$(this).remove();
});
},
"deserialize" : function(str){...},
"urldecode" : function(str) {...}

};



, ,
remove.
, ,
. false,
, .
, , , .
, ", "
, , , remove true.
, remove , true,
fx.removeevent().
, , , fx.addevent(),
, ,
remove false.
, .
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
//
// $.ajax()
var formData = $(this).parents("form").serialize(),
// "submit"
submitVal = $(this).val(),
// ,
remove = false;

PHP jQuery.indb 286

26.10.2010 14:13:16

8. AJAX jQuery

287

// ,
if ( $(this).attr("name")=="confirm_delete" )
{
//
formData += "&action=confirm_delete"
+ "&confirm_delete="+submitVal;
// ,
//
if ( submitVal==", " )
{
remove = true;
}
}
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
// ,
//
if ( remove===true )
{
fx.removeevent();
}
//
fx.boxout();
// ,
//
if ( $("[name=event_id]").val().length==0
&& remove===false )
{
fx.addevent(data, formData);
}
},
error: function(msg) {
alert(msg);
}
});
});

, http://localhost/
Test Event. . , , , ,
(. 8.9).

PHP jQuery.indb 287

26.10.2010 14:13:16

288

III. jQuery PHP-

. 8.9. Test Event

, ,
. .
,
, , ,
.

PHP jQuery.indb 288

26.10.2010 14:13:16

IV


jQuery PHP

, ,
. - ,
, .
. , , , , jQuery.

PHP jQuery.indb 289

26.10.2010 14:13:16

PHP jQuery.indb 290

26.10.2010 14:13:16

, ,
,
,
.
: .
, ,
, ,
.
, .
.
,
.


,
. , . ,
.


Perl-
(Perl-Compatible Regular Expressions, PCRE).
PHP JavaScript, .

PHP jQuery.indb 291

26.10.2010 14:13:16

292

IV. jQuery PHP

. PCRE :
http://ru.wikipedia.org/wiki/PCRE


, . public , regex.php
.
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title> </title>
<style type="text/css">
em {
background-color: #FF0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
</style>
</head>
<body>
<?php
/*
* ,
*
*/
$string = <<<TEST_DATA
<h2> </h2>
<p>
In this document, there is a lot of text that can be matched
using regex. The benefit of using a regular expression is much
more flexible &mdash; albeit complex &mdash; syntax for text
pattern matching.
</p>
<p>
After you get the hang of regular expressions, also called
regexes, they will become a powerful tool for pattern matching.
</p>
<hr />
TEST_DATA;
/*
*

PHP jQuery.indb 292

26.10.2010 14:13:16

9.

293

*/
echo $string;
?>
</body>
</html>

http://localhost/regex.php,
(. 9.1).

. 9.1. ,


<em>, ,
,
.
PHP preg_replace(), str_replace(). , ; ( ) , ; , ,
.
preg_replace($pattern, $replacement, $string);
. p preg_replace() PCRE. PHP
ereg_replace(), POSIX. ,
PHP 5.3.0 ereg .

str_replace()
preg_replace() , , preg_replace()
, ,
, , , , ()
.
.
preg_replace()
, - ,

PHP jQuery.indb 293

26.10.2010 14:13:17

294

IV. jQuery PHP

, .
(/) (#). , cat
/cat/ ( #cat#, %cat%, @cat@ ..).


str_replace() preg_
replace()
regular <em>. regexp.php .
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title> </title>
<style type="text/css">
em {
background-color: #FF0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
</style>
</head>
<body>
<?php
/*
* ,
*
*/
$string = <<<TEST_DATA
<h2> </h2>
<p>
In this document, there is a lot of text that can be matched
using regex. The benefit of using a regular expression is much
more flexible &mdash; albeit complex &mdash; syntax for text
pattern matching.
</p>
<p>
After you get the hang of regular expressions, also called
regexes, they will become a powerful tool for pattern matching.
</p>
<hr />
TEST_DATA;

PHP jQuery.indb 294

26.10.2010 14:13:17

9.

295

/*
* str_replace()
* "regular"
*/
echo str_replace("regular", "<em>regular</em>", $string);
/*
* preg_replace()
* "regular"
*/
echo preg_replace("/regular/", "<em>regular</em>", $string);
?>
</body>
</html>

(. 9.2).

. 9.2. regular

, , regular . , .
,
str_ireplace(), str_replace(), , .
-
preg_replace(), , , .. -

PHP jQuery.indb 295

26.10.2010 14:13:17

296

IV. jQuery PHP

. ,
, .
i.
regexp.php , , ,
.
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title> </title>
<style type="text/css">
em {
background-color: #FF0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
</style>
</head>
<body>
<?php
/*
* ,
*
*/
$string = <<<TEST_DATA
<h2> </h2>
<p>
In this document, there is a lot of text that can be matched
using regex. The benefit of using a regular expression is much
more flexible &mdash; albeit complex &mdash; syntax for text
pattern matching.
</p>
<p>
After you get the hang of regular expressions, also called
regexes, they will become a powerful tool for pattern matching.
</p>
<hr />
TEST_DATA;
/*
* str_ireplace()
* "regular"

PHP jQuery.indb 296

26.10.2010 14:13:17

9.

297

*/
echo str_ireplace("regular", "<em>regular</em>", $string);
/*
* preg_replace()
* "regular"
*/
echo preg_replace("/regular/i", "<em>regular</em>", $string);
?>
</body>
</html>

regular (. 9.3).

. 9.3.

, :
R r.
,
.


, : . () ,
. ( )
,
.
,
. , , , -

PHP jQuery.indb 297

26.10.2010 14:13:17

298

IV. jQuery PHP

. , (\1) ($1).
, str_replace() ,
.
, str_replace() , preg_replace()
.
, , regexp.php .
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title> </title>
<style type="text/css">
em {
background-color: #FF0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
</style>
</head>
<body>
<?php
/*
* ,
*
*/
$string = <<<TEST_DATA
<h2> </h2>
<p>
In this document, there is a lot of text that can be matched
using regex. The benefit of using a regular expression is much
more flexible &mdash; albeit complex &mdash; syntax for text
pattern matching.
</p>
<p>
After you get the hang of regular expressions, also called
regexes, they will become a powerful tool for pattern matching.
</p>
<hr />
TEST_DATA;

PHP jQuery.indb 298

26.10.2010 14:13:17

9.

299

/*
* str_replace()
* "regular"
*/
$check1 = str_replace("regular", "<em>regular</em>", $string);
/*
* str_replace()
* "Regular",
*/
echo str_replace("Regular", "<em>Regular</em>", $check1);
/*
* preg_replace()
* "regular"
*/
echo preg_replace("/(regular)/i", "<em>$1</em>", $string);
?>
</body>
</html>

,
str_replace() . , ,
, (. 9.4).

. 9.4.
. .

PHP jQuery.indb 299

26.10.2010 14:13:17

300

IV. jQuery PHP



. , , (, ,
).
, , . , a c [a-c].
regexp.php , A-C.
; , . , .
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title> </title>
<style type="text/css">
em {
background-color: #FF0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
</style>
</head>
<body>
<?php
/*
* ,
*
*/
$string = <<<TEST_DATA
<h2> </h2>
<p>
In this document, there is a lot of text that can be matched
using regex. The benefit of using a regular expression is much
more flexible &mdash; albeit complex &mdash; syntax for text
pattern matching.
</p>
<p>

PHP jQuery.indb 300

26.10.2010 14:13:17

9.

301

After you get the hang of regular expressions, also called


regexes, they will become a powerful tool for pattern matching.
</p>
<hr />
TEST_DATA;
/*
*
* a-c
*/
$pattern = "/([a-c])/i";
echo preg_replace($pattern, "<em>$1</em>", $string);
/*
*
*/
echo "\n<p> : <strong>$pattern</strong></p>";
?>
</body>
</html>

, ,
(. 9.5). , [abc], [bac] , .
, , (i),
, , [A-Ca-c] .

. 9.5. AC

,
, , ,
(^). , A-C,
/([^a-c])/i (. 9.6).

PHP jQuery.indb 301

26.10.2010 14:13:17

302

IV. jQuery PHP

. 9.6. , AC
. ,
. , , ,
, .



. , ,
, .
 , (- ) (\w). [A-Za-z0-9_].
 (\d). [0-9_].
 (\s).
[ \t\r\n].
, .
, .
 , .
[^A-Za-z0-9_].
 (\D). [^0-9_].
 (\S).
[^ \t\r\n].
. \t, \r n , ; ( ).


, ,
(\b). () , ,
. , stat, thermostat, statistic ecstatic, : /\bstat\b/.

PHP jQuery.indb 302

26.10.2010 14:13:17

9.

303


,
, .
:
 (*)
;
 (+)
;
 ({,})
.
,
, . ,
John John Doe, , : /John( Doe)*/.
, , . ,
, - ,
: /\w+/.
, , . ,
, 0 99,
: /\bd{1,2}\b/.
, , : /(\b\w{4}\b)/ (. 9.7).

. 9.7. ,


, ( , ). (^), ,
.
($), ,
.

PHP jQuery.indb 303

26.10.2010 14:13:17

304

IV. jQuery PHP

,
. , , . , ,
AZ, 09 ,
: /^\w+$/.


, .
, .. (|). . , regex.php -, - , : /\b(\w{3}|\w{6,7})\b/ (. 9.8).

. 9.8. , ,


.
, ,
expression, s .
(?).
, (
).
expression expressions : /(expressions?)/i (. 9.9).


, ,
,
regular expression regex, , .

PHP jQuery.indb 304

26.10.2010 14:13:17

9.

305

. 9.9. s

regex: /(regex)/ (. 9.10).

. 9.10. regex

, es: /(regex(es)?)/i
(. 9.11).

. 9.11. ,

PHP jQuery.indb 305

26.10.2010 14:13:17

306

IV. jQuery PHP

, regular ; : /(reg(ular\s)
?ex(es)?)/i (. 9.12).
, expression es: /(reg(ular\s)?ex(pression|es)?)/i (. 9.13).
, s expression:
/(reg(ular\s)?ex(pressions?|es)?)/i (. 9.14).

. 9.12. regular

. 9.13.

. 9.14.

PHP jQuery.indb 306

26.10.2010 14:13:17

9.

307

. ,
, . , www.regular-expressions.info.



, , , ,
. ,

, .
. , jQuery. , , JavaScript,
JavaScript,
, .



, , ,
. -- ::.


regexp.php,
.
. , .
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=utf-8" />
<title> </title>
<style type="text/css">
em {
background-color: #FF0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;

PHP jQuery.indb 307

26.10.2010 14:13:17

308

IV. jQuery PHP

}
</style>
</head>
<body>
<?php
/*
*
*
*/
$date[] = '2010-01-14 12:00:00';
$date[] = 'Saturday, May 14th at 7pm';
$date[] = '02/03/10 10:00pm';
$date[] = '2010-01-14 102:00:00';
/*
*
*/
$pattern = "/(\d*)/";
foreach ( $date as $d )
{
echo "<p>", preg_replace($pattern, "<em>$1</em>", $d), "</p>";
}
/*
*
*/
echo "\n<p> : <strong>$pattern</strong></p>";
?>
</body>
</html>

, http://localhost/regex.php , (. 9.15).

. 9.15.

PHP jQuery.indb 308

26.10.2010 14:13:18

9.

309


,
, : /^(\d{4})/ (. 9.16).

. 9.16.

,
: /^(\d{4}(-\d{2}))/ (. 9.17).

. 9.17.

, : ,
. ,
, : /^(\d{4}(-\
d{2}){2})/ (. 9.18).

. 9.18.

PHP jQuery.indb 309

26.10.2010 14:13:18

310

IV. jQuery PHP

: /^(\d{4}(-\d{2}){2}
(\d{2}))/ (. 9.19).
. , . (\s) ,
.

,
: /^(\d{4}(-\d{2}){2} (\d{2})(:\d{2})) (. 9.20).
, , , : /^(\d{4}(-\d{2}){2} (\d{2})(:\d{2}){2})$/ (. 9.21).

. 9.19.

. 9.20.

. 9.21.

PHP jQuery.indb 310

26.10.2010 14:13:18

9.

311

,
.

Calendar
Calendar
_validDate().
, ,
, preg_match(), , . ,
, 1,
0.
, TRUE, FALSE.
Calendar, class.calendar.inc.php
, .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
public function displayForm() {...}
public function processForm() {...}
public function confirmDelete($id) {...}
/**
*
*
* @param string $date:
* @return bool: -- TRUE, -- FALSE
*/
private function _validDate($date)
{
/*
*
*
*/

PHP jQuery.indb 311

26.10.2010 14:13:18

312

IV. jQuery PHP

$pattern = '/^(\d{4}(-\d{2}){2} (\d{2})(:\d{2}){2})$/';


/*
* TRUE, -- FALSE
*/
return preg_match($pattern, $date)==1 ? TRUE : FALSE;
}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
private function _adminGeneralOptions() {...}
private function _adminEntryOptions($id) {...}
}
?>


processForm(), _validDate() .

.
processForm() , .
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function __construct($dbo=NULL, $useDate=NULL) {...}
public function buildCalendar() {...}
public function displayEvent($id) {...}
public function displayForm() {...}

PHP jQuery.indb 312

26.10.2010 14:13:18

9.

313

/**
*
*
*
* @return mixed: TRUE
*
*/
public function processForm()
{
/*
* , "action"
*/
if ( $_POST['action']!='event_edit' )
{
return " processForm";
}
/*
*
*/
$title = htmlentities($_POST['event_title'], ENT_QUOTES);
$desc = htmlentities($_POST['event_description'], ENT_QUOTES);
$start = htmlentities($_POST['event_start'], ENT_QUOTES);
$end = htmlentities($_POST['event_end'], ENT_QUOTES);
/*
*
* ,
*/
if ( !$this->_validDate($start)
|| !$this->_validDate($end) )
{
return " !
: -- ::";
}
/*
* ID ,
*/
if ( empty($_POST['event_id']) )
{
$sql = "INSERT INTO `events`
(`event_title`, `event_desc`, `event_start`,
`event_end`)
VALUES
(:title, :description, :start, :end)";
}
/*
* ,
*/
else
{
/*

PHP jQuery.indb 313

26.10.2010 14:13:18

314

IV. jQuery PHP


* ID
*/
$id = (int) $_POST['event_id'];
$sql = "UPDATE `events`
SET
`event_title`=:title,
`event_desc`=:description,
`event_start`=:start,
`event_end`=:end
WHERE `event_id`=$id";
}
/*
*
*
*/
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(":title", $title, PDO::PARAM_STR);
$stmt->bindParam(":description", $desc, PDO::PARAM_STR);
$stmt->bindParam(":start", $start, PDO::PARAM_STR);
$stmt->bindParam(":end", $end, PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
/*
* ID
*/
return $this->db->lastInsertId();
}
catch ( Exception $e )
{
return $e->getMessage();
}

}
public function processForm() {...}
public function confirmDelete($id) {...}
private function _validDate($date) {...}
private function _loadEventData($id=NULL) {...}
private function _createEventObj() {...}
private function _loadEventById($id) {...}
private function _adminGeneralOptions() {...}
private function _adminEntryOptions($id) {...}
}
?>

PHP jQuery.indb 314

26.10.2010 14:13:18

9.

315

, http://localhost/admin.php
(. 9.22).

. 9.22. , ,
. , http://localhost/admin.php, , , JavaScript .
, . ,
JavaScript , ,
.

(. 9.23). ,
JavaScript; ,
.

. 9.23.

PHP jQuery.indb 315

26.10.2010 14:13:18

316

IV. jQuery PHP



JavaScript
. ,
,
jQuery, .

JavaScript


, valid-date.js,
js. , _validDate() Calendar.
, ,
, , match(),
true false,
match() null.
valid_date.js .
//
// (-- ::)
function validDate(date)
{
//
var pattern = /^(\d{4}(-\d{2}){2} (\d{2})(:\d{2}){2})$/;
// true, ,
// false
return date.match(pattern)!=null;
}
. . ,
, ..
, , .


validDate() , JavaScript-
init.js. footer.
inc , .
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript"
src="assets/js/valid-date.js"></script>

PHP jQuery.indb 316

26.10.2010 14:13:18

9.

317

<script type="text/javascript"
src="assets/js/init.js"></script>
</body>
</html>



, validDate(),
init.js, . (start
end ), validDate().
click
, ,
, . , .
init.js .
// ,
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {...}
$("li a").live("click", function(event){...});
$(".admin-options form,.admin")
.live("click", function(event){...});
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
//
// $.ajax()
var formData = $(this).parents("form").serialize(),
// "submit"
submitVal = $(this).val(),
//
start = $(this).siblings("[name=event_start]").val(),
//
end = $(this).siblings("[name=event_end]").val();
// ,
remove = false;

PHP jQuery.indb 317

26.10.2010 14:13:18

318

IV. jQuery PHP


// ,
if ( $(this).attr("name")=="confirm_delete" )
{
//
formData += "&action=confirm_delete"
+ "&confirm_delete="+submitVal;
// ,
//
if ( submitVal=="Yes, Delete It" )
{
remove = true;
}
}
// /,
//
if ( $(this).siblings("[name=action]").val()=="event_edit" )
{
if ( !validDate(start) || !validDate(end) )
{
alert(" !
(-- ::)");
return false;
}
}
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
// ,
//
if ( remove===true )
{
fx.removeevent();
}
//
//
fx.boxout();
// ,
//
if ( $("[name=event_id]").val().length==0
&& remove===false )
{
fx.addevent(data, formData);
}
},
error: function(msg) {
alert(msg);

PHP jQuery.indb 318

26.10.2010 14:13:18

9.

319

}
});
});
$(".edit-form a:contains()")
.live("click", function(event){...});
});

, http://localhost/
,
(. 9.24).

. 9.24. ,

, (. 9.25).
OK , .

PHP jQuery.indb 319

26.10.2010 14:13:18

320

IV. jQuery PHP

. 9.25.

,
. , ,
.
jQuery,
jQuery, jQuery.

PHP jQuery.indb 320

26.10.2010 14:13:18

10

jQuery

jQuery
.
, , (),
, jQuery . ,
jQuery.

jQuery
- jQuery, :
$.yourFunction();

jQuery ,
. , ,
, jQuery, DOM-. , ,
.

jQuery
jQuery
jQuery ( ),
valid-date.js.


jQuery
,
jQuery.
, ,

PHP jQuery.indb 321

26.10.2010 14:13:18

322

IV. jQuery PHP

, $
jQuery.noConflict(). ,
,
.
, , .
.
(function(){
// ...
})();

, .
jQuery,
$, , jQuery.noConflict().
(function($){
// ...
})(jQuery);

$
JavaScript, - .
(function(custom){
// ,
//
custom("p").css("background-color","yellow");
})(jQuery);

jQuery
jQuery, valid-date.
js .
(function($){
// jQuery
$.validDate = function()
{
//
};
})(jQuery);

:
$.validDate();


validDate(), . ,
( ) , -

PHP jQuery.indb 322

26.10.2010 14:13:18

10. jQuery

323

, , .
(function($){
// jQuery
$.validDate = function(date, options)
{
//
};
})(jQuery);

options : ,
. , , , .
(function($){
// jQuery
$.validDate = function(date, options)
{
//
var defaults = {
"pattern" : /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/
};
};
})(jQuery);

,
default $.extend(), , , , , . ,
, , $.extend() , , .
default , .
(function($){
// jQuery
$.validDate = function(date, options)
{
//
var defaults = {
"pattern" : /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/
},
//
//
opts = $.extend(defaults, options);
};
})(jQuery);

PHP jQuery.indb 323

26.10.2010 14:13:18

324

IV. jQuery PHP


,
, , opts.
(function($){
// jQuery
$.validDate = function(date, options)
{
//
var defaults = {
"pattern" : /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/
},
//
//
opts = $.extend(defaults, options);
// , true, -- false
return date.match(opts.pattern)!=null;
};
})(jQuery);

jQuery
,
jQuery . : jquery.[_].js.
, valid_date.js jquery.validDate.js.

,
,
footer.inc.php,
. , .
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript"
src="assets/js/jquery.validDate.js"></script>
<script type="text/javascript"
src="assets/js/init.js"></script>
</body>
</html>

PHP jQuery.indb 324

26.10.2010 14:13:18

10. jQuery

325


, init.js jQuery,
, , .
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
//
fx = {...}
$("li>a").live("click", function(event){...}
$(".admin-options form,.admin").live("click", function(event){...}
//
$(".edit-form input[type=submit]").live("click", function(event){
//
event.preventDefault();
//
// $.ajax()
var formData = $(this).parents("form").serialize(),
// "submit"
submitVal = $(this).val(),
//
start = $(this).siblings("[name=event_start]").val(),
//
end = $(this).siblings("[name=event_end]").val();
// ,
remove = false;
// ,
if ( $(this).attr("name")=="confirm_delete" )
{
//
formData += "&action=confirm_delete"
+ "&confirm_delete="+submitVal;
// ,
//
if ( submitVal=="Yes, Delete It" )
{
remove = true;
}
}
// /,

PHP jQuery.indb 325

26.10.2010 14:13:18

326

IV. jQuery PHP


//
if ( $(this).siblings("[name=action]").val()=="event_edit" )
{
if ( !validDate(start) || !validDate(end) )
{
alert(" ! (--
::)");
return false;
}
}
//
$.ajax({
type: "POST",
url: processFile,
data: formData,
success: function(data) {
// ,
//
if ( remove===true )
{
fx.removeevent();
}
//
//
fx.boxout();
// ,
//
if ( $("[name=event_id]").val().length==0
&& remove===false )
{
fx.addevent(data, formData);
}
},
error: function(msg) {
alert(msg);
}
});

});
$(".edit-form a:contains()")
.live("click", function(event){...};
});

, http//localhost/ ,
. ,
validDate().

PHP jQuery.indb 326

26.10.2010 14:13:18

10. jQuery

327

jQuery
jQuery , , fn jQuery.
.
$(".class").yourPlugin();
. fn jQuery , jQuery
prototype. , .
prototype JavaScript,
: http://www.javascriptkit.com/javatutors/proto.shtml.


,

.
, dateZoom, , , .


, , ,
. js, jquery.dateZoom.js .
(function($){
//
})(jQuery);

fn , .
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{
//
};
})(jQuery);


validDate , , . ,
,
, .

PHP jQuery.indb 327

26.10.2010 14:13:18

328

IV. jQuery PHP

, , , dateZoom. dateZoom
defaults, .
 fontsize. .
110%.
 easing. , . swing.
 duration. . 600.
 callback. , . null.
dateZoom, ,
.
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{
//
};
//
$.fn.dateZoom.defaults = {
"fontsize" : "110%",
"easing" : "swing",
"duration" : "600",
"callback" : null
};
})(jQuery);

dateZoom, :
$.fn.dateZoom.defaults.fontsize = "120%";

, ,
,
, validDate.
, , $.extend().
dateZoom , .
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{

PHP jQuery.indb 328

26.10.2010 14:13:18

10. jQuery

329

// ,
//
var opts = $.extend($.fn.dateZoom.defaults, options);
//
};
//
$.fn.dateZoom.defaults = {
"fontsize" : "110%",
"easing" : "swing",
"duration" : "600",
"callback" : null
};
})(jQuery);


,
jQuery. , jQuery
: , , .each()
this , this.
dateZoom
, , .
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{
// ,
//
var opts = $.extend($.fn.dateZoom.defaults, options);
//
// jQuery
//
return this.each(function(){
//
});
};
//
$.fn.dateZoom.defaults = {
"fontsize" : "110%",
"easing" : "swing",
"duration" : "600",
"callback" : null
};
})(jQuery);

PHP jQuery.indb 329

26.10.2010 14:13:19

330

IV. jQuery PHP


,
, zoom.
defaults,
dateZoom. ,
, .
zoom, dateZoom , .
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{
// ,
//
var opts = $.extend($.fn.dateZoom.defaults, options);
//
// jQuery
//
return this.each(function(){
//
});
};
//
$.fn.dateZoom.defaults = {
"fontsize" : "110%",
"easing" : "swing",
"duration" : "600",
"callback" : null
};
// ,
//
$.fn.dateZoom.zoom = function(element, size, opts)
{
//
};
})(jQuery);

, , , .
. , ,
,
, options.


.animate(), .dequeue()

PHP jQuery.indb 330

26.10.2010 14:13:19

10. jQuery

331

clearQueue(). , , .
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{
// ,
//
var opts = $.extend($.fn.dateZoom.defaults, options);
//
// jQuery
//
return this.each(function(){
//
});
};
//
$.fn.dateZoom.defaults = {
"fontsize" : "110%",
"easing" : "swing",
"duration" : "600",
"callback" : null
};
// ,
//
$.fn.dateZoom.zoom = function(element, size, opts)
{
$(element).animate({
"font-size" : size
},{
"duration" : opts.duration,
"easing" : opts.easing,
"complete" : opts.callback
})
.dequeue() //
.clearQueue(); //
//
};
})(jQuery);
. .dequeue()
, .clearQueue().
, , .

PHP jQuery.indb 331

26.10.2010 14:13:19

332

IV. jQuery PHP


, .each(), , ,
jQuery, . dateZoom
hover .
,
dateZoom, zoom.
fontsize defaults .
zoom ,
.
,
.css(), .
.hover(),
dateZoom , .
(function($){
// ,
// ,
//
$.fn.dateZoom = function(options)
{
// ,
//
var opts = $.extend($.fn.dateZoom.defaults, options);
//
// jQuery
//
return this.each(function(){
//
var originalsize = $(this).css("font-size");
// hover.
// ,
// --
$(this).hover(function(){
$.fn.dateZoom.zoom(this, opts.fontsize, opts);
},
function(){
$.fn.dateZoom.zoom(this, originalsize, opts);
});
});
};
//
$.fn.dateZoom.defaults = {
"fontsize" : "110%",
"easing" : "swing",
"duration" : "600",
"callback" : null
};

PHP jQuery.indb 332

26.10.2010 14:13:19

10. jQuery

333

// ,
//
$.fn.dateZoom.zoom = function(element, size, opts)
{
$(element).animate({
"font-size" : size
},{
"duration" : opts.duration,
"easing" : opts.easing,
"complete" : opts.callback
})
.dequeue() //
.clearQueue(); //
//
};
})(jQuery);


, .

, .


,
footer.inc.php, .
validDate, , dateZoom
init.js.
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript"
src="assets/js/jquery.validDate.js"></script>
<script type="text/javascript"
src="assets/js/jquery.dateZoom.js"></script>
<script type="text/javascript"
src="assets/js/init.js"></script>
</body>
</html>


,
.dateZoom() . init.js, .
fontsize 13px, .dateZoom() , ,

PHP jQuery.indb 333

26.10.2010 14:13:19

334

IV. jQuery PHP

"li a". init.js,


, :
jQuery(function($){
var processFile = "assets/inc/ajax.inc.php",
fx = {...}
// dateZoom
$.fn.dateZoom.defaults.fontsize = "13px";
//
// zoom effect
$("li a")
.dateZoom()
.live("click", function(event){
// view.php
//
event.preventDefault();
// "active"
$(this).addClass("active");
// href
var data = $(this)
.attr("href")
.replace(/.+?\?(.*)$/, "$1"),
//
//
modal = fx.initModal();
//
$("<a>")
.attr("href", "#")
.addClass("modal-close-btn")
.html("&times;")
.click(function(event){
//
fx.boxout(event);
})
.appendTo(modal);
//
$.ajax({
type: "POST",
url: processFile,
data: "action=event_view&" + data,
success: function(data){
//
fx.boxin(data, modal);
},
error: function(msg) {

PHP jQuery.indb 334

26.10.2010 14:13:19

10. jQuery

335

alert(msg);
}
});
});
$(".admin-options form,.admin")
.live("click", function(event){...});
//
$(".edit-form input[type=submit]")
.live("click", function(event){...});
$(".edit-form a:contains()")
.live("click", function(event){...};
});

, http://localhost/
, dateZoom
(. 10.1).

. 10.1.

PHP jQuery.indb 335

26.10.2010 14:13:19

336

IV. jQuery PHP

jQuery, ,
, ,
. , ,
jQuery .
! PHP jQuiery
-, . !

PHP jQuery.indb 336

26.10.2010 14:13:19


A
AJAX 15, 76, 233, 248

C
CSRF 159
CSS 21, 54, 144, 162

DOM 21
42

F
Firebug 17

J
JavaScript 15, 223
jQuery 15, 221
JSON 44

P
PCRE 291
PDO 119
phpMyAdmin 118

X
XAMPP 18

195
63
29, 54

257
90

86
87

extends 95
private 102
protected 99

PHP jQuery.indb 337

public 99
static 103
105
90

91
159
88
jQuery

add() 41
addClass() 59
after() 48
andSelf() 41
animate() 66
append() 45, 236
appendTo() 48
attr() 54
before() 48
bind() 72
children() 37
clearQueue() 331
closest() 37
contents() 42
css() 55
data() 58
delay() 69
dequeue() 331
detach() 53
die() 74
each() 62, 329
end() 42
eq() 34
error() 70
fadeIn() 65, 242
fadeOut() 65, 240
fadeTo() 65
filter() 35
find() 37
first() 35
get() 80
getJSON() 80

26.10.2010 14:13:19

338

getScript() 81
has() 36
hasClass() 60
height() 60
hide() 64, 244
html() 56
innerHeight() 61
innerWidth() 61
insertAfter() 48
insertBefore() 48
is() 36
last() 35
live() 74, 252
load() 81
map() 62
next() 38
nextAll() 38
nextUntil() 38
noConflict() 322
not() 35
one() 74
outerHeight() 61
outerWidth() 61
parent() 40
parents() 40
parentsUntil() 40
post() 80
prepend() 45
prependTo() 48
prev() 39
prevAll() 39
prevUntil() 39
ready() 71
remove() 53
removeAttr() 55
removeClass() 59
scroll() 70
serialize() 253
show() 64
siblings() 39
slice() 36
slideDown() 65
slideToggle() 65
slideUp() 65
stop() 69
text() 56

PHP jQuery.indb 338

toggle() 75
toggleClass() 59
trigger() 75
unbind() 72
unload() 71
unwrap() 50
val() 58
width() 60
wrap() 49
wrapAll() 50
wrapInner() 53
PHP

delay() 266
getProperty() 88, 95
getTimezoneOffset() 264
setMinutes() 264
setProperty() 88
90

__construct() 90
__destruct() 91
__toString() 93
222, 226, 271
252
229

95

98, 99
34
86
Date 262
fn 327
prototype 327
-
() 85
229, 232

16, 321
16, 321
327

195
227, 291

26.10.2010 14:13:19

293
295
297
303
300

22
24
253
128
70
71
72

$_POST 157, 160
$_SESSION 160

jQuery

ajax() 233, 243


jQuery() 21, 273
PHP

__autoload() 111
ereg_replace() 293
preg_replace() 293
sleep() 199
String() 265
str_replace() 293
unset() 92
var_dump() 86

195

33, 321, 329

162, 176, 224

26
29
28
27
30
- 29
159, 168, 183
188, 200
15

PHP jQuery.indb 339

339

263

UNIX 127

26.10.2010 14:13:19

JQUERY


JavaScript
..

www.williamspublishing.com

ISBN 978-5-8459-1603-7

. 340


JavaScript-
jQuery.

,

,
,

DOM-
,
.


AJAX

.


XMLHttpRequest,
AJAX,

jQuery.


jQuery UI,
,

,
jQuery JavaScript.


,
.

-
PHP MYSQL



www.williamspublishing.com

ISBN 978-5-8459-1574-0

. 341


,
HTML

,
, ,


.


PHP MySQL

e-.

.


,



.

PHP MySQL,

-,
e-

.

,
.

PHP MYSQL

2-
,
,




Web-

( PHP,
Apache MySQL),
,


Windows
Linux.
,
,
,


.

,


.

www.dia l ektika .co m

ISBN 978#5#8459#1640-2

. 342

PHP: ,

,
2-

www.williamspublishing.com

ISBN 978-5-8459-1586-3

. 343


-


PHP.

-
PHP,
,
,
, ,

.
,
,
,
,
.

,
.




PHP.


,

,
,
PHP: Phing, PHPUnit2,
PHPDocumentor, PEAR CVS.

WEB 2.0

PHP

www.williamspublishing.com

ISBN 978-5-8459-1590-0

. 344



,
Web 2.0



.

PHP
Zend Framework
Smarty Template
Engine.


JavaScript- Prototype
Scriptaculous.

MySQL PostgreSQL
,


Ajax.


-

.



19-

www.williamspublishing.com

ISBN 978!5!8459!1668-6

. 345


:
Core i Intel Phenom AMD,
SSD
, ,
,
, DDR3
Windows 7,
.

, ,
.
:
Intel AMD,
Intel Core i
AMD Phenom;
DTX Mini ITX;
,
NVIDIA
ATI/AMD;
, SSD,

.

,

2-
. ,
. ,
,
.

www.williamspublishing.com

ISBN 978-5-8459-1349-4

. 346

,
,


,


.



.
,
,

,

.
, ,


,



.



,


.


MICROSOFT OFFICE EXCEL 2010
..

ice

10

B Microsoft
Ofce

Excel
2010
B

Microsoft Ofce

Excel 2010

..


Excel 2010,

 ,

www.dialektika.com

-:





(Excel 2003)

www.dialektika.com

23.09.2010 15:08:01

ISBN 978-5-8459-1669-3

. 347



Excel 2010.


Excel 2010,
,

.


Excel.



Excel 2010,


,

.


.
,


Excel 2010
.

MICROSOFT WINDOWS
SERVER 2008 R2

,
,
,
,

. 348

www.williamspublishing.com



Windows Server 2008
R2, Active Directory,
, ,
Windows Server
2008 R2 Windows Server
2003/2008, ,
, ,
,

.


Windows Server 2008 R2

Windows Server
2008 R2,
Hyper-V DirectAccess

Failover Clustering.

,
,

-
Windows Server 2008 R2.


.

ISBN 978584591653-2

-

..
..

www.williamspublishing.com

ISBN 978-5-8459-1679-2

. 349


-.



, .


,


. ,



.
,
,

, ,



,
-.

HTML, XHTML CSS

5-

www.dia l ektika .co m

ISBN 978584591676-1

. 350




-
(HTML),
HTML
(XHTML)
(CSS).

, ,
, ,

,


-.



.

-
.

++

, ,
, ,

++

. C++

www.williamspublishing.com

01.07.2010 18:47:47

ISBN 978-5-8459-1650-1

. 351



,

.



C++,


.

,

,


.




-
.


.

C# 4.0

www.williamspublishing.com

ISBN 978-5-8459-1684-6

. 352


C# 4.0 ,
.NET,

:
, , , ,
, , ,
, , , , ,
,

.

C#, PLINQ, TPL,
,
.




,


.

, C#.

Оценить