diff options
author | Mateja <mail@matejamaric.com> | 2020-10-02 01:37:59 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2020-10-02 01:37:59 +0200 |
commit | 6a290235781cbca58dd1d102d030110b4eb0c6c9 (patch) | |
tree | eb4d1818ae6b322f6c69c2f3ead696532e125376 | |
parent | 110967fe02d337872508b2bba9cd48db138987ab (diff) | |
download | old-php-yota-6a290235781cbca58dd1d102d030110b4eb0c6c9.tar.gz old-php-yota-6a290235781cbca58dd1d102d030110b4eb0c6c9.zip |
Prototype finished!
-rw-r--r-- | test/edit.inc.php | 60 | ||||
-rw-r--r-- | test/index.php | 20 | ||||
-rw-r--r-- | test/request.js | 32 |
3 files changed, 92 insertions, 20 deletions
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 @@ <?php -echo $_POST['action']; -if($_SERVER["REQUEST_METHOD"] == "POST" && isset(_POST['email'])) { +if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST["action"] == "update") { + try { + $conn = new PDO("mysql:host=localhost;dbname=testdb", "testuser", "testpass"); + $conn->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 . "<br>"; 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; } </style> </head> @@ -83,9 +82,12 @@ try { echo "<td><div contenteditable=\"true\" class=\"edit\">" . $row['from'] . "</div></td>"; echo "<td><div contenteditable=\"true\" class=\"edit\">" . $row['to'] . "</div></td>"; echo "<td><div contenteditable=\"true\" class=\"edit\">" . $row['name'] . "</div></td>"; + //echo "<td><div contenteditable=\"true\" class=\"edit\">" . $row['from'] . "</div></td>"; + //echo "<td><div contenteditable=\"true\" class=\"edit\">" . $row['to'] . "</div></td>"; + //echo "<td><div contenteditable=\"true\" class=\"edit\">" . $row['name'] . "</div></td>"; echo "<td>"; - echo "<button onclick=\"subAction('save', this)\">Save</button>"; - echo "<button onclick=\"subAction('cancel', this)\">Cancel</button>"; + echo "<button onclick=\"subAction('update', this)\">Update</button>"; + echo "<button onclick=\"subAction('restore', this)\">Restore</button>"; echo "<button onclick=\"subAction('delete', this)\">Delete</button>"; echo "</td></tr>\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); |