From 6a290235781cbca58dd1d102d030110b4eb0c6c9 Mon Sep 17 00:00:00 2001 From: Mateja Date: Fri, 2 Oct 2020 01:37:59 +0200 Subject: Prototype finished! --- test/edit.inc.php | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- test/index.php | 20 ++++++++++--------- test/request.js | 32 ++++++++++++++++++++--------- 3 files changed, 92 insertions(+), 20 deletions(-) (limited to 'test') diff --git a/test/edit.inc.php b/test/edit.inc.php index 3309a7b..a08a48a 100644 --- a/test/edit.inc.php +++ b/test/edit.inc.php @@ -1,7 +1,63 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $stmt = $conn->prepare("UPDATE tbl1 SET `name`=:name, `from`=:from, `to`=:to WHERE id=:id"); + $stmt->bindParam(':id', $_POST["id"]); + $stmt->bindParam(':name', $_POST["name"]); + $stmt->bindParam(':from', $_POST["from"]); + $stmt->bindParam(':to', $_POST["to"]); + $stmt->execute(); + + $data->action=$_POST["action"]; + echo json_encode($data); + } + catch (PDOException $e) { + echo $e->getMessage(); + } +} + +if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST["action"] == "restore") { + try { + $conn = new PDO("mysql:host=localhost;dbname=testdb", "testuser", "testpass"); + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $stmt = $conn->prepare("SELECT * FROM tbl1 WHERE id=:id"); + $stmt->bindParam(':id', $_POST["id"]); + $stmt->execute(); + $row = $stmt->fetch(); + + $data->action=$_POST["action"]; + $data->id = $row["id"]; + $data->from = $row["from"]; + $data->to = $row["to"]; + $data->name = $row["name"]; + + echo json_encode($data); + } + catch (PDOException $e) { + echo $e->getMessage(); + } +} + +if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST["action"] == "delete") { + try { + $conn = new PDO("mysql:host=localhost;dbname=testdb", "testuser", "testpass"); + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $stmt = $conn->prepare("DELETE FROM tbl1 WHERE id=:id"); + $stmt->bindParam(':id', $_POST["id"]); + $stmt->execute(); + + $data->action=$_POST["action"]; + echo json_encode($data); + } + catch (PDOException $e) { + echo $e->getMessage(); + } +} + +if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['email'])) { $email = clear_input($_POST['email']); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "Email is not valid: " . $email . "
"; diff --git a/test/index.php b/test/index.php index fb6e942..4e0d7a5 100644 --- a/test/index.php +++ b/test/index.php @@ -40,23 +40,22 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["name"]) && isset($_POS border-collapse: collapse; } th, td { - border: 1px solid black; + border: 1px solid #ccc; padding: 0.3rem; } tbody tr:nth-child(even) { - background-color: #ccc; + background-color: #ddd; } thead { - color: white; - background-color: black; + color: #333; + background-color: #ccc; } .edit { padding: 2px; border-top: 1px solid #333; border-left: 1px solid #333; - border-bottom: 1px solid #666; - border-right: 1px solid #666; - border-radius: 3px; + border-bottom: 1px solid #bbb; + border-right: 1px solid #bbb; } @@ -83,9 +82,12 @@ try { echo "
" . $row['from'] . "
"; echo "
" . $row['to'] . "
"; echo "
" . $row['name'] . "
"; + //echo "
" . $row['from'] . "
"; + //echo "
" . $row['to'] . "
"; + //echo "
" . $row['name'] . "
"; echo ""; - echo ""; - echo ""; + echo ""; + echo ""; echo ""; echo "\n"; } diff --git a/test/request.js b/test/request.js index bfcf592..d423e85 100644 --- a/test/request.js +++ b/test/request.js @@ -3,14 +3,12 @@ function subAction(action, btn) { trData = trDom.children; trId = trData[0].innerHTML; - trFrom = trData[1].innerHTML; - trTo = trData[2].innerHTML; - trName = trData[3].innerHTML; - - console.log(trId); - console.log(trFrom); - console.log(trTo); - console.log(trName); + //trFrom = trData[1].innerHTML; + //trTo = trData[2].innerHTML; + //trName = trData[3].innerHTML; + trFrom = trData[1].firstElementChild.innerHTML; + trTo = trData[2].firstElementChild.innerHTML; + trName = trData[3].firstElementChild.innerHTML; //for (var i = 0, len = trData.length - 1; i < len; i++) { //console.log(i + ": " + trData[i].innerHTML); @@ -24,7 +22,23 @@ function subAction(action, btn) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { - document.getElementById("notice").innerHTML = this.responseText; + try { + var response = JSON.parse(this.responseText) + if (response.action == "update") { + document.getElementById("notice").innerHTML = this.responseText; + } + else if (response.action == "restore") { + trData[1].firstElementChild.innerHTML = response.from; + trData[2].firstElementChild.innerHTML = response.to; + trData[3].firstElementChild.innerHTML = response.name; + } + else if (response.action == "delete") { + document.getElementById("notice").innerHTML = JSON.stringify(response); + } + } catch { + console.log(this.responseText); + document.getElementById("notice").innerHTML = "Bad input data!"; + } } } xhttp.open("POST", "edit.inc.php", true); -- cgit v1.2.3