Hi,
Today i will show very easy way to read POST variables from Javascript using PHP.
1 2 3 | <script type="text/javascript">// <![CDATA[ var post_json = "<?php echo json_encode($_POST); ?>"; // ]]></script> |
We encode all POST vars with php to javascript JSON.
Now we can access the vars in simple way:
1 | post_json.var_name; |
Have a nice day,
Garry
jQuery Communicator Singleton (Manage & Cache ajax requests)
Hey,
I wrote this class for cache ajax request if the same request sent twice.
The second reason why i use this class is a like there is only one exit point
from the application.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var instantiated;
var retry = new Array();
var cached_data = new Array();
function init (){
return {
dispatch:function(url, callback){
var self = this;
if (Communicator.getInstance().cached_data[url]) {
callback(Communicator.getInstance().cached_data[url]);
return;
}
if (!retry[url]) {
retry[url] = 0;
}
var _rq = $.ajax({
url: url,
timeout: 30000,
success: function(_data){
Communicator.getInstance().cached_data[url] = _data;
callback(_data);
},
error: function(){
if (retry[url] < 3) {
retry[url]++;
dispatch(url, callback, retry[url]);
} else {
callback("");
}
}
});
},
cached_data: {}
}
}
return {
getInstance :function(){
if (!instantiated){
instantiated = init();
}
return instantiated;
}
}
})()
Using the class
2
3
4
5
6
7
8
9
10
11
12
var error = false;
try {
data = eval("("+data+")");
} catch(err) {
error = true;
}
if (!error) {
console.log(data);
}
});
Have fun
Garry