![]()
Snippets of A |
|
|
What are snippets? |
|
function ajaxFunction(url, myfunction, post){//1.0.1 var anAjaxRequest=null; var workaround='x'+'m'+'l';/*for copy & paste purposes: some servers are wary of x m l strings*/ try{anAjaxRequest=new window[workaround.toUpperCase()+'HttpRequest']();}catch(e){ try{anAjaxRequest=new ActiveXObject("Ms"+workaround+"2."+workaround.toUpperCase()+"HTTP");}catch(e){ try{anAjaxRequest=new ActiveXObject("Microsoft."+workaround.toUpperCase()+"HTTP");}catch(e){ alert("Ajax not supported"); return null; } } } anAjaxRequest.onreadystatechange=function(){ if( (anAjaxRequest.readyState==4 || anAjaxRequest.readyState=='complete') && (anAjaxRequest.status==200 || anAjaxRequest.status==100)){ return (myfunction)?myfunction(anAjaxRequest.responseText):anAjaxRequest.responseText; } } anAjaxRequest.open((post)?"POST":"GET", url, true); anAjaxRequest.setRequestHeader('Content-Type', (post)?'application/x-www-form-urlencoded':'text/'+workaround); /*allow encodeURIComponent*/ if(post && typeof post=='string'){ if(post.indexOf('&')!==false && post.indexOf('=')!==false){/*allow encodeURIComponent*/ post=post.split('&'); for(var p=0; p<post.length; p++){ post[p]=post[p].substring(0, post[p].indexOf('='))+'='+encodeURIComponent( post[p].substring(post[p].indexOf('=')+1) ); } post=post.join('&'); } else if(post.indexOf('=')!==false){post=post.substring(0, post.indexOf('='))+'='+encodeURIComponent( post.substring(post[p].indexOf('=')+1) );}; }; /*allowed encodeURIComponent*/ anAjaxRequest.send(post||null); return false; /*keep this comment to reuse freely http://www.fullposter.com/?1*/}Remove colors
null.
POST and in such case it must be a string already arranged in pairs of keys and values connected by the sign =, each pair separated by the other by an ampersand sign &
false or null if ajax appears not supported by the client browser. Its onreadystatechange returns what your response handler function returns, or if no such function is provided it returns a string representing the php echo.
&) should be present in the string to post only as separator of the key/values tuples. In case your values already contain ampersands not in the role of separators, you should rebuild the last argument to pass to the function taking care of applying yourself encodeURIComponent to its values - and eventually, in such case, you must remove from this function the part that applies encodeURIComponent, currently easily identified by comments sorrounding it beginning and end.
encodeURIComponent issues which sooner or later is likely if not even bound to happen (thence, I am posting this shorter version only to prove how easy it can be to have a working ajax, though far from bullet proof):function ajaxFunction(url, myfunction, post){//1.0.1shorter var anAjaxRequest=null; var workaround='x'+'m'+'l';/*for copy & paste purposes: some servers are wary of x m l strings*/ try{anAjaxRequest=new window[workaround.toUpperCase()+'HttpRequest']();}catch(e){ try{anAjaxRequest=new ActiveXObject("Ms"+workaround+"2."+workaround.toUpperCase()+"HTTP");}catch(e){ try{anAjaxRequest=new ActiveXObject("Microsoft."+workaround.toUpperCase()+"HTTP");}catch(e){ alert("Ajax not supported"); return null; } } } anAjaxRequest.onreadystatechange=function(){ if( (anAjaxRequest.readyState==4 || anAjaxRequest.readyState=='complete') && (anAjaxRequest.status==200 || anAjaxRequest.status==100)){ return (myfunction)?myfunction(anAjaxRequest.responseText):anAjaxRequest.responseText; } } anAjaxRequest.open((post)?"POST":"GET", url, true); anAjaxRequest.setRequestHeader('Content-Type', (post)?'application/x-www-form-urlencoded':'text/'+workaround); anAjaxRequest.send(post||null); return false; /*keep this comment to reuse freely http://www.fullposter.com/?1*/}Remove colors
<?php print $_POST['boh2']; ?>Remove colors
foo.php, this is an example of use:
<script> //include your ajax function here, then: function myCustomFunction(phpEcho){ alert(phpEcho);/*or anything else handling phpEcho*/ } ajaxFunction("foo.php", myCustomFunction, 'boh=hallo&boh2=hαλλo2'); </script>Remove colors