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
//connection data
$server = "anysql.itcollege.ee";
$user = "team4";
$password = "rw_353MIl_e";
$database = "WT_4";
//connect using mysqli object-oriented style
$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
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);
if (!$result) die ("Database access failed");
*/
$query = "TRUNCATE TABLE toDoList;";
if(!$link->query($query)) {
echo "(".$link->errno.")".$link->error;
if($result->num_rows <= 0) {
echo "No task was deleted";
}
$link -> close();
?>
<?php
include_once "dbconnection.php";
$query ="DELETE FROM toDoList WHERE task=?";
$query ="DELETE FROM toDoList WHERE ID=?";
$query = $link->prepare($query);
$query->bind_param('s', $_GET['task']);
$query->bind_param('s', $_GET['id']);
$query->execute();
$query->close();
......
......@@ -2,17 +2,20 @@
header('Content-type: application/json');
include_once "dbconnection.php";
$query = "SELECT task FROM toDoList;";
$query = "SELECT ID,task FROM toDoList;";
$result = $link -> query($query);
if (!$result) die ("Database access failed");
$data = array();
for ($i = 0; $i < $result->num_rows; ++$i) {
//$task = array();
$row = $result->fetch_array(MYSQLI_NUM);
array_push($data, $row[0]);
$data[$row[0]] = $row[1];
}
header('Content-Type: application/json');
echo json_encode($data);
$link->close();
?>
var i = 0;
//var i = 0;
/*
//add new row to list after submitting
function addRow(){
if (document.getElementById('add-task').value!=''){
i++;
var title = document.getElementById('add-task').value;
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>';
document.getElementById('doList').appendChild(node);
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);
}
}
*/
//delete all tasks
function deleteAll() {
......@@ -43,23 +59,46 @@ $("#imageUpload").change(function() {
readURL(this);
});
//display input from database
$(document).ready(function() {
//display input from the database
$.ajax({
dataType: 'JSON',
url: 'display.php',
success: function(data) {
var items = [];
//var items = [];
$.each( data, function(key, val) {
i++;
var title = val;
//i++;
//var title = val
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);
})
}
});
});
/*
//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
$(document).ready(function() {
......@@ -70,7 +109,9 @@ $(document).ready(function() {
$.ajax({
type: "GET",
url: "delete-complete.php",
data: {task: $(this).next().children(".check--label-text").text()}
data: {
id: $(this).next().attr("for")
}
});
$(this).parent().remove();
});
......
......@@ -7,8 +7,16 @@ $("#addAction").submit(function(event) {
type:"POST",
data:{
"task": task },
success: function() {
success: function(data) {
$("#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>
<html lang="en">
<head>
......@@ -29,9 +39,9 @@
<div class="extra-content" id="extraContent">
<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 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>
<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">
</form>
</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";
$query = "INSERT INTO toDoList (task) VALUES (?) ";
$query = $link->prepare($query);
$query -> bind_param('s', $_POST['task']);
$query -> execute();
$query->bind_param('s', $_POST['task']);
$query ->execute();
$last_id = mysqli_insert_id($link);
echo $last_id;
$query -> close();
$link -> close();
?>
......@@ -8,9 +8,9 @@
<div class="row">
<div class="column">
<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>
<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>
<label for="password">Password&nbsp</label>
<input type="password" id="password" name="password" autocomplete="off" required>
......
<?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");
?>
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