Advertisement
gundambison

loogin ajax

Aug 4th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.18 KB | None | 0 0
  1. Testing LOGIN
  2. <form  action="login_process.php" method="post" id="my_form">
  3.     <input id='usr_name' type="text" class="form-control" name="username" placeholder="Enter Username" />
  4.     <input id='usr_pass' type="password" class="form-control" name="password" placeholder="Enter password" />
  5.     <button type="button" class="cancelbtn" onclick="reload2()" >LANJUT</button>
  6. </form>
  7. <hr/>
  8. <div id='error_message'>PESAN MUNCUL DISINI</div>
  9. <div id='error'>KODE ERROR DISINI</div>
  10.  
  11.  
  12. <script type="text/javascript" charset="utf8" src="js/ajax-0.1c.js"></script>
  13. <script>
  14.         url = "032-login.php";
  15.         function reload2() {
  16.             data = {
  17.                 'user': document.getElementById('usr_name').value,
  18.                 'password': document.getElementById('usr_pass').value
  19.             }
  20.             console.log(data);
  21.             obj = ajaxPostJson(url, data, false);
  22.             console.log(obj);
  23.             if (obj.error != false) {
  24.                 document.getElementById('error').innerHTML = obj.status;
  25.                 document.getElementById('error_message').innerHTML = obj.message;
  26.             } else {
  27.                 res = obj.result;
  28.                 document.getElementById('error').innerHTML = res.error;
  29.                 document.getElementById('error_message').innerHTML = res.error_message;
  30.             }
  31.         }
  32. //==================================
  33. function ajaxGetShowId(url, targetId, async=true,debug=0){
  34.     var xhttp=xmlHttp();
  35.    
  36.     if(async!=true)async=false;
  37.     xhttp.open("GET", url, async);
  38.     xhttp.onreadystatechange = function () {
  39.         if(debug==1){
  40.             console.log(xhttp);
  41.         }
  42.         if (xhttp.readyState != 4 || xhttp.status != 200){
  43.             return 0;
  44.         }
  45.         else{
  46.             document.getElementById(targetId).innerHTML=xhttp.responseText;
  47.         }
  48.     };
  49.     xhttp.send();
  50. /*bila TIDAK menggunakan async maka bisa mengembalikan hasil*/
  51.     response={detail:xhttp, text:xhttp.responseText, state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  52.     return response;
  53. }
  54.  
  55. function ajaxGetJson(url,async=false, debug=0){
  56.     var xhttp=xmlHttp();
  57.    
  58.     if(async!=true)async=false;
  59.     xhttp.open("GET", url, async);
  60.     xhttp.onreadystatechange = function () {
  61.         if(debug==1){
  62.             console.log(xhttp);
  63.         }
  64.         if (xhttp.readyState != 4 || xhttp.status != 200){
  65.             return 0;
  66.         }
  67.     };
  68.     xhttp.send();
  69.     response={state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  70.     text=xhttp.responseText;
  71.     try {
  72.         json=JSON.parse(text);
  73.         response.result=json;
  74.         response.error=false;
  75.         response.message='OK';
  76.     }
  77.     catch(err) {
  78.         response.result=false;
  79.         response.error=err;
  80.         response.message=err.message;
  81.     }
  82.     return response;
  83. }
  84.  
  85. function ajaxPostJson(url,data=array(),async=false, debug=0){
  86.     var xhttp=xmlHttp();
  87.    
  88.     if(async!=true)async=false;
  89.     xhttp.open("POST", url, async);
  90. //  xhttp.setRequestHeader('Content-Type', 'application/json');
  91.     xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  92.  
  93.     xhttp.onreadystatechange = function () {
  94.         if(debug==1){
  95.             console.log(xhttp);
  96.         }
  97.         if (xhttp.readyState != 4 || xhttp.status != 200){
  98.             return 0;
  99.         }
  100.     };
  101.     params= param(data); console.log(params);
  102.     xhttp.send( params );//JSON.stringify(data) );
  103.     response={state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  104.     text=xhttp.responseText;
  105.     try {
  106.         json=JSON.parse(text);
  107.         response.result=json;
  108.         response.error=false;
  109.         response.message='OK';
  110.     }
  111.     catch(err) {
  112.         response.result=false;
  113.         response.error=err;
  114.         response.message=err.message;
  115.     }
  116.     return response;
  117. }
  118.  
  119. function xmlHttp(){
  120.     if (window.XMLHttpRequest) {
  121.         xhttp = new XMLHttpRequest();
  122.         } else {
  123.         // code for IE6, IE5
  124.         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  125.     //new ActiveXObject("MSXML2.XMLHTTP.3.0")  
  126.     }
  127.     return xhttp;
  128. }
  129.  
  130. function param(object) {
  131.     var encodedString = '';
  132.     for (var prop in object) {
  133.         if (object.hasOwnProperty(prop)) {
  134.             if (encodedString.length > 0) {
  135.                 encodedString += '&';
  136.             }
  137.             encodedString += encodeURI(prop + '=' + object[prop]);
  138.         }
  139.     }
  140.     return encodedString;
  141. }
  142.  
  143. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement