Skip to content

Commit 6131785

Browse files
committed
Clean up dynamic-samples/category.php. Fixes jquery-archive#5314.
Fixes there errors: PHP Notice: Use of undefined constant vehicles - assumed 'vehicles' in ./docs/pages/dynamic-samples/category.php on line 53 PHP Notice: Use of undefined constant name - assumed 'name' in ./docs/pages/dynamic-samples/category.php on line 54 PHP Notice: Use of undefined constant description - assumed 'description' in ./docs/pages/dynamic-samples/category.php on line 55 PHP Notice: Use of undefined constant items - assumed 'items' in ./docs/pages/dynamic-samples/category.php on line 56 PHP Notice: Use of undefined constant name - assumed 'name' in ./docs/pages/dynamic-samples/category.php on line 58 PHP Notice: Use of undefined constant name - assumed 'name' in ./docs/pages/dynamic-samples/category.php on line 61 PHP Notice: Use of undefined constant name - assumed 'name' in ./docs/pages/dynamic-samples/category.php on line 64 PHP Notice: Undefined index: id in ./docs/pages/dynamic-samples/category.php on line 74 PHP Notice: Undefined index: in ./docs/pages/dynamic-samples/category.php on line 82 PHP Notice: Undefined index: HTTP_X_REQUESTED_WITH in ./docs/pages/dynamic-samples/category.php on line 88 Also added charset in Content-Type header and used json_encode instead of homebrew unescaped json encoder. Follows-up cac2032
1 parent 044b7cb commit 6131785

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

docs/pages/dynamic-samples/animals.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<script src="../../../js/jquery.js"></script>
99
<script src="../../../docs/_assets/js/jqm-docs.js"></script>
1010
<script src="../../../js/"></script>
11-
1211
</head>
1312

1413
<body>

docs/pages/dynamic-samples/category.php

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,60 @@
88
// are just using some static in-memory data.
99

1010
$category_data = array(
11-
animals => array(
12-
name => "Animals",
13-
description => "All your favorites from aardvarks to zebras.",
14-
items => array(
11+
"animals" => array(
12+
"name" => "Animals",
13+
"description" => "All your favorites from aardvarks to zebras.",
14+
"items" => array(
1515
array(
16-
name => "Pets",
16+
"name" => "Pets",
1717
),
1818
array(
19-
name => "Farm Animals",
19+
"name" => "Farm Animals",
2020
),
2121
array(
22-
name => "Wild Animals",
22+
"name" => "Wild Animals",
2323
)
2424
)
2525
),
26-
colors => array(
27-
name => "Colors",
28-
description => "Fresh colors from the magic rainbow.",
29-
items => array(
26+
"colors" => array(
27+
"name" => "Colors",
28+
"description" => "Fresh colors from the magic rainbow.",
29+
"items" => array(
3030
array(
31-
name => "Blue",
31+
"name" => "Blue",
3232
),
3333
array(
34-
name => "Green",
34+
"name" => "Green",
3535
),
3636
array(
37-
name => "Orange",
37+
"name" => "Orange",
3838
),
3939
array(
40-
name => "Purple",
40+
"name" => "Purple",
4141
),
4242
array(
43-
name => "Red",
43+
"name" => "Red",
4444
),
4545
array(
46-
name => "Yellow",
46+
"name" => "Yellow",
4747
),
4848
array(
49-
name => "Violet",
49+
"name" => "Violet",
5050
)
5151
)
5252
),
53-
vehicles => array(
54-
name => "Vehicles",
55-
description => "Everything from cars to planes.",
56-
items => array(
53+
"vehicles" => array(
54+
"name" => "Vehicles",
55+
"description" => "Everything from cars to planes.",
56+
"items" => array(
5757
array(
58-
name => "Cars",
58+
"name" => "Cars",
5959
),
6060
array(
61-
name => "Planes",
61+
"name" => "Planes",
6262
),
6363
array(
64-
name => "Construction",
64+
"name" => "Construction",
6565
)
6666
)
6767
)
@@ -70,44 +70,35 @@
7070
// Get the name of the category to display from
7171
// the query params for the script.
7272

73-
$category_name = '';
74-
if ( $_GET[ 'id' ] ) {
73+
if ( isset( $_GET[ 'id' ] ) ) {
7574
$category_name = $_GET[ 'id' ];
75+
} else {
76+
$category_name = false;
7677
}
7778

7879
// Now get the category data, by name, from our in-memory
7980
// dictionary. This is the part where a script normally fetches
8081
// the data from a database.
8182

82-
$category_obj = $category_data[ $category_name ];
83+
if ( $category_name && isset( $category_data[ $category_name ] ) ) {
84+
$category_obj = $category_data[ $category_name ];
85+
} else {
86+
$category_obj = null;
87+
}
8388

8489
// Now figure out how the script is being called. If it's being
8590
// called via XmlHttpRequest, then send the data back as JSON.
8691
// If not, then send it back as a list in an HTML document.
8792

88-
if( $_SERVER[ "HTTP_X_REQUESTED_WITH" ] && $_SERVER[ "HTTP_X_REQUESTED_WITH" ] ==="XMLHttpRequest" ) {
93+
if ( isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest' ) {
8994
// Data should be written out as JSON.
90-
header("Content-type: application/json");
91-
if ( !$category_obj ) {
92-
echo 'null';
93-
} else {
94-
echo '{"name":"' . $category_obj[ 'name' ]
95-
. '","description":"' . $category_obj[ 'description' ]
96-
. '","items":[';
95+
header('Content-type: application/json; charset=UTF-8');
96+
echo json_encode( $category_obj );
97+
exit;
98+
}
9799

98-
$arr = $category_obj[ 'items' ];
99-
$count = count($arr);
100-
for ( $i = 0; $i < $count; $i++ ) {
101-
if ( $i ) {
102-
echo ",";
103-
}
104-
echo '{"name":"' . $arr[ $i ][ 'name' ] . '"}';
105-
}
106-
echo "]}";
107-
}
108-
} else {
109-
// Data should be written out as HTML.
110-
header("Content-type: text/html");
100+
// Data should be written out as HTML.
101+
header('Content-type: text/html; charset=UTF-8');
111102
?>
112103
<!DOCTYPE HTML>
113104
<html>
@@ -121,7 +112,13 @@
121112
</head>
122113
<body>
123114
<div data-role="page" data-add-back-btn="true">
124-
<div data-role="header"><h1><?php if ( $category_obj ) { echo $category_obj['name']; } else { echo "No Match"; } ?></h1></div>
115+
<div data-role="header"><h1><?php
116+
if ( $category_obj ) {
117+
echo htmlspecialchars( $category_obj[ 'name' ] );
118+
} else {
119+
echo 'No Match';
120+
}
121+
?></h1></div>
125122
<div data-role="content">
126123
<?php
127124
if ( !$category_obj ) {
@@ -130,13 +127,13 @@
130127
<?php
131128
} else {
132129
?>
133-
<p><?php echo $catgory_object['description']; ?></p>
130+
<p><?php echo htmlspecialchars( $catgory_object['description'] ); ?></p>
134131
<ul data-role="listview" data-inset="true">
135132
<?php
136133
$arr = $category_obj[ 'items' ];
137134
$count = count($arr);
138135
for ( $i = 0; $i < $count; $i++ ) {
139-
echo "\t\t\t<li>" . $arr[ $i ][ 'name' ] . "</li>\n";
136+
echo "\t\t\t<li>" . htmlspecialchars( $arr[ $i ][ 'name' ] ) . "</li>\n";
140137
}
141138
?>
142139
</ul>
@@ -147,4 +144,3 @@
147144
</div>
148145
</body>
149146
</html>
150-
<?php }

0 commit comments

Comments
 (0)