Erg leuk ben nu met action triggering bezig voor een communicatie site werkt echt perfect. maakt gebruik van dit script (
http://twilightuniverse.com/2005/05/sack-of-ajax/) hieronder een voorbeeldje.
Dit script hieronder loopt automatisch en kijkt of de waardes van de laatste id verschillen als ze verschillen haalt hij de acties op en voert ze uit.
action zetten: setAction.php?action=getTest|dit is de alert text
setAction.php bevat gewoon een standaard insert scriptje:
insert into ajactions (action) values ('$_GET[action]');
in de getAction() wordt de naam van de funtie "getTest" uit de database via eval aangeroepen. alles wat je in action zet wordt als functie aangeroepen.
Wat linkies!!:
http://www.ajaxblog.com/
http://www.ajaxian.com/
http://www.ajaxmatters.com/
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| switch ($_POST[action]){
case "checkActions":
getLastAction();
break;
case "getActions":
getActions($_POST[lastId]);
break;
}
function getLastAction(){
global $conn_id;
$query="select * from ajactions order by Id desc limit 1";
$result=mysql_query($query, $conn_id) or die('er gaat iets four'.mysql_error());
$row=mysql_fetch_array($result);
$res=$row[Id];
echo $res;
}
function getActions($id){
global $conn_id;
$query="select * from ajactions where Id > $id";
$result=mysql_query($query, $conn_id);
$i=0;
while($row=mysql_fetch_array($result)){
$res.="actions[$i]='$row[Id]|$row[action]'\n";
$i++;
}
echo $res;
} |
javascript sectie
JavaScript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| <html>
<head>
<script type="text/javascript" src="sacks.js"></script>
<script type="text/javascript">
function timer(){
setInterval('readArray()', 10000);
}
function getActions(lastId){
var mydata = 'action=getActions&lastId='+lastId;
ajaxs = new sack('doSomething.php');
ajaxs.onCompletion = getAction;
ajaxs.runAJAX(mydata);
}
function getAction(){
actions = new Array();
var i = 0;
eval(ajaxs.response);
for (i=0; i<actions.length; i++){
action=actions[i].split("|");
eval(action[1]+'(action[2])');
}
}
function getTest(test){
alert(test);
}
function getArray(){
var mydata = 'action=checkActions';
ajaxx = new sack('doSomething.php');
ajaxx.onCompletion = getArrays;
ajaxx.runAJAX(mydata);
}
function getArrays(){
action=ajaxx.response;
}
function readArray(){
var mydata = 'action=checkActions';
ajax = new sack('doSomething.php');
ajax.onCompletion = myFn;
ajax.runAJAX(mydata);
}
function myFn(){
var check = ajax.response;
if (check!=action){
getActions(action)
}
getArray();
}
</script>
</head>
<body onload="timer(); getArray()">
</body>
</html> |
[
Voor 11% gewijzigd door
Verwijderd op 20-05-2005 12:49
]