-
Notifications
You must be signed in to change notification settings - Fork 12
/
AJAX-examples
52 lines (44 loc) · 4.23 KB
/
AJAX-examples
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
NOTICE: Please, include "SHOW_waiting" function ( from https://github.com/ttodua/useful-javascript/blob/master/during-request-show-%22WAIT%22 )
so, during request, you will see "PLEASE WAIT" message.
=============================================================================================================
===============================METHOD 1: POST or GET request (without Jquery) ===============================
=============================================================================================================
USAGE: myyAjaxRequest("d=kap&e=po", "http://mysite.com/myInfo.php", "POST", "alert('Hi,completed: ' + responseee);", true , "plz,WaitMeeeee");
<script type="text/javascript">
//##################### AJAX EXAMPLES ############## https://github.com/ttodua/useful-javascript/ ####################
function myyAjaxRequest(parameters, url, method, YOUR_PASSED_CODES, ShowBlackground, YourWaitMessage){ pp_ShowBlackground= ShowBlackground || false;
var method= (method || "get").toLowerCase(); var url = (url =='') ? window.location.href : url; if (method=="get") { url= url + ( (url.indexOf('?') <= -1) ? '?' : '&') +parameters+'&CacheAvoider=' + Math.random();}
if(pp_ShowBlackground) { var z = document.createElement("div"); z.id = "my_waiting_box_888"; z.innerHTML= '<div style="background-color:black; color:white; opacity:0.9;height:8000px; left:0px; position:fixed; top:0px; width:100%; z-index:1007;" id="ppa_shadow"> <div style="position:absolute; top:200px;left:49%; z-index: 1008;" id="ppa_load">'+ ( YourWaitMessage || '<span id="ppa_phrase" style="color:grey;font-size:24px;">LOADING...</span>')+'<br/></div></div>'; document.body.appendChild(z); }
try{try{var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} catch( e ){var xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}} catch(e){var xmlhttp = new XMLHttpRequest();} xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4){ if(pp_ShowBlackground) {var x=document.getElementById("my_waiting_box_888"); x.parentNode.removeChild(x);} responseee = xmlhttp.responseText; eval(YOUR_PASSED_CODES); } //xmlhttp.readyState + xmlhttp.status//
}; xmlhttp.open(method,url, true); if (method == "get"){xmlhttp.send(null);} else if (method == "post"){xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send(parameters);}
}
//########################################################################################################################
</script>
=============================================================================================
===============================METHOD 2: POST request (with Jquery) =========================
=============================================================================================
USAGE: myRequest_2("myParameter1=kap&e=po", "filee.php", "POST", "alert('Hii,completed' + responseee );");
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
//##################### AJAX EXAMPLES ############## https://github.com/ttodua/useful-javascript/ ####################
function myRequest_2(parameters,target,method, passedFunction){ alert('wait 1 sec');
$.post(target, parameters ,
function(data,status){
if (status == "success") { alert("completed!");
responseee="\n\nRESPONSE:\n" + data; eval(passedFunction); //execute any codes
/* if you want get serialized array from php, then:
1) in php, use this: echo json_encode( array('myname'=>'Frank', 'surname'=>'Klobert') );
2) parse with this: arrayed = $.parseJSON(data); var z=''; for (property in arrayed) {z += property+": "+arrayed[property]+";\n ";} alert(z);
*/
}
})
}
</script>
===================================== OTHER NOTES ==========================
================================================================================================
***p.s. instead of manually typing ("myParameter1=kap&e=po"), you can get all fieldnames and their values directly from a FORM:
jQuery:
var stringg = $("#formId").serialize();
Simple Javascript
var stringg = serialize(document.forms["FORM_NAME"]); //but include custom "FORM SERIALIZE" function, found at: https://github.com/tazotodua/useful-javascript