From 798924e8933f0d15753d84f3d7eaade44b5002d6 Mon Sep 17 00:00:00 2001
From: adyanf <adyanaufalf@gmail.com>
Date: Fri, 6 Oct 2017 14:54:52 +0700
Subject: [PATCH] adding ajax handler

---
 css/profile.css            |  2 +-
 editpreferredlocations.php |  1 +
 js/editlocation.js         | 22 ++++++++++++++++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/css/profile.css b/css/profile.css
index ea790e7..94dbd5c 100644
--- a/css/profile.css
+++ b/css/profile.css
@@ -260,7 +260,7 @@ td:nth-of-type(2n-1) {
 }
 
 .data-location .delete {
-    color: #B33A3A;
+    color: red;
     font-weight: bold;
     float: right;
     margin-right: 10px;
diff --git a/editpreferredlocations.php b/editpreferredlocations.php
index ac74cd8..ca6ad71 100644
--- a/editpreferredlocations.php
+++ b/editpreferredlocations.php
@@ -39,6 +39,7 @@ if(isset($_POST["submit"])) {
 $sql = "SELECT location FROM driver_locations WHERE ID=$id_active";
 
 $result = mysqli_query($conn, $sql);
+echo "<script>var id = " . $id_active . ";</script>"
 ?>
 
 <!DOCTYPE html>
diff --git a/js/editlocation.js b/js/editlocation.js
index 52f5298..2123dc4 100644
--- a/js/editlocation.js
+++ b/js/editlocation.js
@@ -10,7 +10,7 @@ function editLocation(elmt){
     row.children[2].children[1].children[0].onclick = function(){ cancelLocation(elmt, textBefore)};
 }
 
-function saveLocation(elmt, text){
+function saveLocation(elmt, textBefore){
     var row = elmt.parentElement.parentElement.parentElement;
     row.children[1].children[0].disabled = true;
     row.children[2].children[0].children[0].innerHTML = "mode_edit";
@@ -19,6 +19,16 @@ function saveLocation(elmt, text){
     row.children[2].children[1].children[0].style.color = "red";
     row.children[2].children[0].children[0].onclick = function(){ editLocation(elmt)};
     row.children[2].children[1].children[0].onclick = function(){ deleteLocation(elmt)};
+    var text = encodeURI(row.children[1].children[0].value);
+    textBefore = encodeURI(textBefore);
+    var xmlhttp = new XMLHttpRequest();
+    if(!xmlhttp){
+        return;
+    }
+    var url = "editlocationhandler.php?id_active="+ id + "&update=" + text + "&location=" + textBefore;
+    xmlhttp.open("GET", url, true);
+    xmlhttp.send();
+    location.reload();
 }
 
 function cancelLocation(elmt, text){
@@ -36,6 +46,14 @@ function cancelLocation(elmt, text){
 function deleteLocation(elmt){
     if (confirm("Are you sure deleting the location ?") == true) {
         var row = elmt.parentElement.parentElement.parentElement;
-
+        var text = encodeURI(row.children[1].children[0].value);
+        var xmlhttp = new XMLHttpRequest();
+        if(!xmlhttp){
+            return;
+        }
+        var url = "editlocationhandler.php?id_active="+ id + "&delete=" + text;
+        xmlhttp.open("GET", url, true);
+        xmlhttp.send();
+        location.reload();        
     }
 }
\ No newline at end of file
-- 
GitLab