DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Powerfull Ajax Engine
// This is my ajax engine. You can now do everything you want with ajax.
To use it, it's very simple. Launch an ajax process like this example :
// nx is a GET request, only for small and fixed datas
nx('engine.php?action=youraction&type='+parseInt(type)+'&id='+parseInt(id)+'&nav='+parseInt(nav)+'&lw='+parseInt(LW()),'engine');
// or
nx("_engine.php?action=json","engine");
or
// np is a POST request, designed to send huge amout of variable datas
np('newpost.php','action=post&forumid='+parseInt(post[1])+'&titre='+titre+'&msg='+str+'&postid='+parseInt(post[2]),'posting');
My AJAX Engine :
function h(){var c=false,g='',d='';c=new XMLHttpRequest();this.r=function(x){d=x};this.appendData=function(a,b){g+=(g.length==0)?a+'='+escape(b):'&'+a+'='+escape(b)};this.s=function(a,b,e){b=b.toUpperCase();c.onreadystatechange=function(){if(c.readyState==4&&c.status==200){if(typeof e=='function'){e(c);return}switch(d){
case'engine':
// an example to grab a JSON block sent by PHP. The callback will land here.
myJSONblock.push(JSON.parse(c.responseText));
return;break;
case'posting':
// for example, you can forward the return of your PHP post template contained into "c.responseText" into a div with getElementById. You can do what you want. The callback will land here.
document.getElementById('yourID').innerHTML = c.responseText;
return;break;
}}};if(b=='GET'){c.open('GET',a);c.send(null)}else if(b=='POST'){c.open('POST',a);c.setRequestHeader('Content-Type','application/x-www-form-urlencoded');c.send(g)}return true};return this}function nx(f,x){var a=new h();a.r(x);a.s(f,'GET');return true};function np(f,a,x){var b=new h();b.r(x);b.appendData(a+'&');b.s(f,'POST');return true};
Your PHP file can look like this, for example :
<?php header('Content-type: text/html; charset=UTF-8');require_once( 'settings.php' );
switch ($_GET['action']){
case "forum": // ** FORUM **
checkLogin ( '2' );
if (!$_SESSION['logged_in']) {
echo SendJSON(array_merge(array("some javascript commands"),htmlTOarray("forum_face.php")));
} else {
ob_start();
eval("?>".file_get_contents("forum_".$forum[(int)$_GET['type']].".php")."<?");
$feval = ob_get_contents();
ob_end_clean();
echo SendJSON(array_merge(array("some javascript commands"),navbar(),array(" ")));
}
break;
default:
switch ($_POST['action']){
case "posting": // ** POST **
checkLogin ( '2' );
if (!$_SESSION['logged_in']) {
echo SendJSON(array_merge(array("some javascript commands"),htmlTOarray("forum_face.php")));
} else {
if (!(int)$_POST['postid']) $_POST['postid'] = "";
$query = $db->query ( "INSERT INTO dso_forums.posts (`id`, `parent`, `msg_parent`, `reponses`, `vues`, `note`, `auteur`, `titre`, `contenu`, `derniers`, `access`, `password`, `date`) VALUES (NULL, ".$db->qstr ( (int)$_POST['forumid'] ).", ".$db->qstr ( (int)$_POST['postid'] ).", '', '', '10', ".$db->qstr ( $_SESSION['username'] ).", ".$db->qstr ( $_POST['titre'] ).", ".$db->qstr ( $_POST['msg'] ).", '', '', '', '".date('Y-m-d H:i:s')."' )" );
}
break;
default:}
}
?>
Some functions aren't posted here, but I will give one of mine that is very usefull by not being disturbed by char encoding :
<?php
function SendJSON($arr) {
return json_encode(array_map("utf8_encode",$arr));
}
?>
Another functions can be posted if you request them ;=)





