Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | date_default_timezone_set('Europe/Copenhagen'); |
||
3 | $bday = strtotime("5 September 1983"); |
||
4 | $age = (date('z')-date('z', $bday))/1000+date('Y')-date('Y', $bday); |
||
5 | |||
6 | function return_array() { |
||
7 | global $age; |
||
8 | return array("name" => "Anders", "age" => $age); |
||
9 | } |
||
10 | |||
11 | function return_object() { |
||
12 | global $age; |
||
13 | class MyObj { |
||
14 | var $name, $age; |
||
15 | |||
16 | function MyObj($name, $age) { |
||
17 | $this->name = $name; |
||
18 | $this->age = $age; |
||
19 | } |
||
20 | } |
||
21 | $o = new MyObj("Anders", $age); |
||
22 | return $o; |
||
23 | } |
||
24 | |||
25 | function return_int() { |
||
26 | global $age; |
||
27 | return floor($age); |
||
28 | } |
||
29 | |||
30 | function return_float() { |
||
31 | global $age; |
||
32 | return $age; |
||
33 | } |
||
34 | |||
35 | function return_string() { |
||
36 | global $age; |
||
37 | return "Anders is ".floor($age)." years old."; |
||
38 | } |
||
39 | |||
40 | require("sajax.php"); |
||
41 | $sajax_request_type = "GET"; |
||
42 | // $sajax_debug_mode = true; |
||
43 | $sajax_failure_redirect = "http://sajax.info/sajaxfail.html"; |
||
44 | sajax_export("return_array", "return_object", "return_int", "return_float", "return_string"); |
||
45 | sajax_handle_client_request(); |
||
46 | ?> |
||
47 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
||
48 | <html xmlns="http://www.w3.org/1999/xhtml"> |
||
49 | <head> |
||
50 | <title>Sajax PHP return types example</title> |
||
51 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
||
52 | <script type="text/javascript" src="json2.stringify.js"></script> |
||
53 | <script type="text/javascript" src="json_stringify.js"></script> |
||
54 | <script type="text/javascript" src="json_parse_state.js"></script> |
||
55 | <script type="text/javascript" src="sajax.js"></script> |
||
56 | <script type="text/javascript"><!-- |
||
57 | <?php sajax_show_javascript(); ?> |
||
58 | function display_result(val) { |
||
59 | var repr; |
||
60 | |||
61 | repr = ""; |
||
62 | repr += "Type: " + typeof val + "\n"; |
||
63 | repr += "Value: "; |
||
64 | if (typeof val == "object" || |
||
65 | typeof val == "array") { |
||
66 | repr += "{ "; |
||
67 | for (var i in val) |
||
68 | repr += i + ": " + val[i] + ", "; |
||
69 | repr = repr.substr(0, repr.length-2) + " }"; |
||
70 | } else { |
||
71 | repr += val; |
||
72 | } |
||
73 | alert(repr); |
||
74 | } |
||
75 | //--> |
||
76 | </script> |
||
77 | </head> |
||
78 | <body> |
||
79 | <button onclick="x_return_array(display_result);">Return as array (will become an object)</button> |
||
80 | <button onclick="x_return_object(display_result);">Return as object</button> |
||
81 | <button onclick="x_return_int(display_result);">Return as int</button> |
||
82 | <button onclick="x_return_float(display_result);">Return as float/double</button> |
||
83 | <button onclick="x_return_string(display_result);">Return as string</button> |
||
84 | </body> |
||
85 | </html> |