Commit 15421a6a by sopham

delete tasks by id, php user authentication

parent e476aa2f
<?php
session_start();
require_once "dbconnection.php";
if ($stmt = $link->prepare('SELECT ID,password FROM users WHERE username= ?')) {
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
if ($_POST['password'] === $password) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
header('Location:index.php');
} else {
echo "Incorrect password";
}
}
$stmt->close();
}
?>
<?php <?php
//connection data
$server = "anysql.itcollege.ee"; $server = "anysql.itcollege.ee";
$user = "team4"; $user = "team4";
$password = "rw_353MIl_e"; $password = "rw_353MIl_e";
$database = "WT_4"; $database = "WT_4";
//connect using mysqli object-oriented style
$link = new mysqli($server, $user, $password, $database); $link = new mysqli($server, $user, $password, $database);
if($link -> connect_error) die("Connecion to DB failed: ". $link -> connect_error);
//error-handling
if($link -> connect_error)
die("Connecion to DB failed: ". $link -> connect_error);
?> ?>
<?php <?php
include_once "dbconnection.php"; include_once "dbconnection.php";
/*
$link = new mysqli($server, $user, $password, $database);
if($link -> connect_error) die("Connecion to DB failed: ". $link -> connect_error);
$query = "SELECT task FROM toDoList;"; $query = "TRUNCATE TABLE toDoList;";
$result = $link -> query($query); $result = $link -> query($query);
if (!$result) die ("Database access failed"); if($result->num_rows <= 0) {
*/ echo "No task was deleted";
$query = "TRUNCATE TABLE toDoList;";
if(!$link->query($query)) {
echo "(".$link->errno.")".$link->error;
} }
$link -> close();
?> ?>
<?php <?php
include_once "dbconnection.php"; include_once "dbconnection.php";
$query ="DELETE FROM toDoList WHERE task=?"; $query ="DELETE FROM toDoList WHERE ID=?";
$query = $link->prepare($query); $query = $link->prepare($query);
$query->bind_param('s', $_GET['task']); $query->bind_param('s', $_GET['id']);
$query->execute(); $query->execute();
$query->close(); $query->close();
......
...@@ -2,17 +2,20 @@ ...@@ -2,17 +2,20 @@
header('Content-type: application/json'); header('Content-type: application/json');
include_once "dbconnection.php"; include_once "dbconnection.php";
$query = "SELECT task FROM toDoList;"; $query = "SELECT ID,task FROM toDoList;";
$result = $link -> query($query); $result = $link -> query($query);
if (!$result) die ("Database access failed"); if (!$result) die ("Database access failed");
$data = array(); $data = array();
for ($i = 0; $i < $result->num_rows; ++$i) { for ($i = 0; $i < $result->num_rows; ++$i) {
//$task = array();
$row = $result->fetch_array(MYSQLI_NUM); $row = $result->fetch_array(MYSQLI_NUM);
array_push($data, $row[0]); $data[$row[0]] = $row[1];
} }
header('Content-Type: application/json'); header('Content-Type: application/json');
echo json_encode($data); echo json_encode($data);
$link->close(); $link->close();
?> ?>
var i = 0;
//var i = 0;
/*
//add new row to list after submitting //add new row to list after submitting
function addRow(){ function addRow(){
if (document.getElementById('add-task').value!=''){ if (document.getElementById('add-task').value!=''){
i++; i++;
var title = document.getElementById('add-task').value; var title = document.getElementById('add-task').value;
var node = document.createElement('div'); var node = document.createElement('div');
node.innerHTML = '<input id="option' + i + '" type="checkbox" class="hidden" name="checkbox"><label for="option' + i + '" class="check--label"><span class="check--label-box"></span><span class="check--label-text">'+ title +'</span>'; node.innerHTML = '<input id="' + i + '" type="checkbox" class="hidden" name="checkbox"><label for="' + i + '" class="check--label"><span class="check--label-box"></span><span class="check--label-text">'+ title +'</span>';
var lastElement = parseInt(document.getElementById("doList").lastElementChild.getAttribute("id"));
if (!isNaN(lastElement)) {
node.setAttribute("id", lastElement + 1);
}
else {
node.setAttribute("id", 0);
}
$.ajax({
dataType: 'JSON',
url: 'get-id.php',
success: function(data){
})
//node.setAttribute("id", new_id);
document.getElementById('doList').appendChild(node); document.getElementById('doList').appendChild(node);
} }
} }
*/
//delete all tasks //delete all tasks
function deleteAll() { function deleteAll() {
...@@ -43,23 +59,46 @@ $("#imageUpload").change(function() { ...@@ -43,23 +59,46 @@ $("#imageUpload").change(function() {
readURL(this); readURL(this);
}); });
//display input from database
$(document).ready(function() { $(document).ready(function() {
//display input from the database
$.ajax({ $.ajax({
dataType: 'JSON', dataType: 'JSON',
url: 'display.php', url: 'display.php',
success: function(data) { success: function(data) {
var items = []; //var items = [];
$.each( data, function(key, val) { $.each( data, function(key, val) {
i++; //i++;
var title = val; //var title = val
var node = document.createElement('div'); var node = document.createElement('div');
node.innerHTML = '<input id="option' + i + '" type="checkbox" class="hidden" name="checkbox"><label for="option' + i + '" class="check--label"><span class="check--label-box"></span><span class="check--label-text">' + title + '</span>'; node.innerHTML = '<input id="' + key + '" type="checkbox" class="hidden" name="checkbox"><label for="' + key + '" class="check--label"><span class="check--label-box"></span><span class="check--label-text">' + val + '</span>';
document.getElementById('doList').appendChild(node); document.getElementById('doList').appendChild(node);
}) })
} }
}); });
}); });
/*
//add id into the each task after load page
$.ajax({
dataType:'JSON',
url: 'add-id.php',
success: function(data) {
var i = 0;
var id = [];
$.each(data, function(key, val) {
id.push(val);
})
$('#doList').children('div').each(function () {
if($(this).attr('class') == "progress-container") { return; }
else {
$(this).attr('id', id[i]);
i++;
}
})
}
});
});
*/
//delete completed tasks //delete completed tasks
$(document).ready(function() { $(document).ready(function() {
...@@ -70,7 +109,9 @@ $(document).ready(function() { ...@@ -70,7 +109,9 @@ $(document).ready(function() {
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: "delete-complete.php", url: "delete-complete.php",
data: {task: $(this).next().children(".check--label-text").text()} data: {
id: $(this).next().attr("for")
}
}); });
$(this).parent().remove(); $(this).parent().remove();
}); });
......
...@@ -7,8 +7,16 @@ $("#addAction").submit(function(event) { ...@@ -7,8 +7,16 @@ $("#addAction").submit(function(event) {
type:"POST", type:"POST",
data:{ data:{
"task": task }, "task": task },
success: function() { success: function(data) {
$("#add-task").val(""); $("#add-task").val("");
var new_id = data;
if (document.getElementById('add-task').value='') {
var title = document.getElementById('add-task').value();
var node = document.createElement('div');
node.innerHTML = '<input id="' + new_id + '" type="checkbox" class="hidden" name="checkbox"><label for="' + new_id + '" class="check--label"><span class="check--label-box"></span><span class="check--label-text">' + title + '</span>';
node.setAttribute("id", new_id);
document.getElementById('doList').appendChild(node);
}
} }
}); });
}); });
......
<?php
session_start();
if(!isset($_SESSION['loggedin'])) {
session_destroy();
$params = session_get_cookie_params();
setcookie(session_name(), '', 0, $params['path'], $params['domain'], $params['secure'], isset($params['httponly']));
header('Location: login_page.php');
exit();
}
?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
...@@ -29,9 +39,9 @@ ...@@ -29,9 +39,9 @@
<div class="extra-content" id="extraContent"> <div class="extra-content" id="extraContent">
<a href="javascript:window.print()"><i class="glyphicon glyphicon-print"></i> Print List</a> <a href="javascript:window.print()"><i class="glyphicon glyphicon-print"></i> Print List</a>
<a id="delete-complete"><i class="fa fa-trash w3-large"></i> Completed Tasks</a> <a id="delete-complete"><i class="fa fa-trash w3-large"></i> Completed Tasks</a>
<a onclick="deleteAll();" id="delete-button"><i class="fa fa-trash w3-large"></i> Delete All</a> <a id="delete-button"><i class="fa fa-trash w3-large"></i> Delete All</a>
</div><br> </div><br>
<form action="login_check.php" method="POST" id="action"> <form action="logout.php" method="POST" id="action">
<input type="submit" class="logout-button" name="logout"value="Logout"> <input type="submit" class="logout-button" name="logout"value="Logout">
</form> </form>
</div> </div>
......
<?php
if(isset($_POST['logout'])) {
session_name($user);
session_destroy();
header('Location: login_page.php');
exit;
}
if(isset($_POST['add-button'])){
$addTask = $_POST['add-task'];
$file = fopen("taskList.txt","a+") or die("Unable to open file");
$s = $addTask."\r\n";
fputs($file,$s) or die("Unable to open save");
fclose($file);
header('Location: index.php');
}
?>
...@@ -3,10 +3,11 @@ include_once "dbconnection.php"; ...@@ -3,10 +3,11 @@ include_once "dbconnection.php";
$query = "INSERT INTO toDoList (task) VALUES (?) "; $query = "INSERT INTO toDoList (task) VALUES (?) ";
$query = $link->prepare($query); $query = $link->prepare($query);
$query -> bind_param('s', $_POST['task']);
$query -> execute();
$query->bind_param('s', $_POST['task']); $last_id = mysqli_insert_id($link);
$query ->execute(); echo $last_id;
$query -> close(); $query -> close();
$link -> close(); $link -> close();
?> ?>
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<div class="row"> <div class="row">
<div class="column"> <div class="column">
<div class="card"> <div class="card">
<form name="input" action="login_check.php" method="POST"> <form name="input" action="authentication.php" method="POST">
<label for="username">Username</label> <label for="username">Username</label>
<input type="text" id="username" name="username" pattern="[a-zA-Z][a-zA-Z0-9-_\.]{1,20}" required> <input type="text" id="username" name="username" pattern="[a-zA-Z][a-zA-Z0-9-_\.]{1,20}" required>
<br> <br>
......
<?php
session_start();
session_unset();
session_destroy();
$params = session_get_cookie_params();
setcookie(session_name(), '', 0, $params['path'], $params['domain'], $params['secure'], isset($params['httponly']));
header("Location: login_page.php");
?>
@charset "UTF-8"; @charset "UTF-8";
* { * {
...@@ -181,6 +182,10 @@ input[type=text] { ...@@ -181,6 +182,10 @@ input[type=text] {
appearance: none; appearance: none;
} }
.list {
display: flex;
}
.check--label { .check--label {
display: flex; display: flex;
justify-content: flex-start; justify-content: flex-start;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment