CREATE TABLE `todos` (
`id` int(11) NOT NULL,
`title` text NOT NULL,
`date_time` datetime NOT NULL DEFAULT current_timestamp(),
`checked` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `todos`
ADD PRIMARY KEY (`id`);
ALTER TABLE `todos`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
index.php
//index.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>PHP MYSQL PDO Todo List with JQuery AJAX</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" href="css/style.css"> </head> <body> <?php require 'dbconn.php'; ?> <div class="container"><h1>PHP MYSQL PDO Todo List with JQuery AJAX</h1> <div class="row"> <div class="col-2"></div> <div class="col-9" style="border-radius: 5px;background-color:#fff;padding:30px;"> <form action="add.php" method="POST" autocomplete="off"> <?php if(isset($_GET['mess']) && $_GET['mess'] == 'error'){ ?> <p><input type="text" name="title" class="form-control form-control-lg" placeholder="This field is required" /></p> <p><button type="submit" class="btn btn-primary btn-lg">Add <span>+</span></button></p> <?php }else{ ?> <p><input type="text" name="title" class="form-control form-control-lg" placeholder="What do you need to do?" /></p> <p><button type="submit" class="btn btn-primary btn-lg">Add <span>+</span></button></p> <?php } ?> </form> </div> <div class="col-2"></div> </div> <?php $todos = $conn->query("SELECT * FROM todos ORDER BY id DESC"); ?> <div class="row"> <div class="col-2"></div> <div class="col-9" style="border-radius: 5px;background-color:#fff;padding:30px;margin-top:20px;"> <?php if($todos->rowCount() <= 0){ ?> <div class="todo-item"> <div class="empty"> <h1>No Record </h1> </div> </div> <?php } ?> <?php while($todo = $todos->fetch(PDO::FETCH_ASSOC)) { ?> <div class="todo-item"> <span id="<?php echo $todo['id']; ?>" class="remove-to-do">x</span> <?php if($todo['checked']){ ?> <input type="checkbox" class="check-box" data-todo-id ="<?php echo $todo['id']; ?>" checked /> <h2 class="checked"><?php echo $todo['title'] ?></h2> <?php }else { ?> <input type="checkbox" data-todo-id ="<?php echo $todo['id']; ?>" class="check-box" /> <h2><?php echo $todo['title'] ?></h2> <?php } ?> <br> <small>created: <?php echo $todo['date_time'] ?></small> </div> <?php } ?> </div> <div class="col-2"></div> </div> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <!-- https://code.jquery.com/ --> <script> $(document).ready(function(){ $('.remove-to-do').click(function(){ const id = $(this).attr('id'); $.post("remove.php", { id: id }, (data) => { if(data){ $(this).parent().hide(600); } } ); }); $(".check-box").click(function(e){ const id = $(this).attr('data-todo-id'); $.post('check.php', { id: id }, (data) => { if(data != 'error'){ const h2 = $(this).next(); if(data === '1'){ h2.removeClass('checked'); }else { h2.addClass('checked'); } } } ); }); }); </script> </body> </html>dbconn.php
//dbconn.php <?php $sName = "localhost"; $uName = "root"; $pass = ""; $db_name = "testingdb"; try { $conn = new PDO("mysql:host=$sName;dbname=$db_name", $uName, $pass); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo "Connection failed : ". $e->getMessage(); }add.php
//add.php <?php if(isset($_POST['title'])){ require 'dbconn.php'; $title = $_POST['title']; if(empty($title)){ header("Location: index.php?mess=error"); }else { $stmt = $conn->prepare("INSERT INTO todos(title) VALUE(?)"); $res = $stmt->execute([$title]); if($res){ header("Location: index.php?mess=success"); }else { header("Location: index.php"); } $conn = null; exit(); } }else { header("Location: index.php?mess=error"); }check.php
//check.php <?php if(isset($_POST['id'])){ require 'dbconn.php'; $id = $_POST['id']; if(empty($id)){ echo 'error'; }else { $todos = $conn->prepare("SELECT id, checked FROM todos WHERE id=?"); $todos->execute([$id]); $todo = $todos->fetch(); $uId = $todo['id']; $checked = $todo['checked']; $uChecked = $checked ? 0 : 1; $res = $conn->query("UPDATE todos SET checked=$uChecked WHERE id=$uId"); if($res){ echo $checked; }else { echo "error"; } $conn = null; exit(); } }else { header("Location: index.php?mess=error"); }remove.php
//remove.php <?php if(isset($_POST['id'])){ require 'dbconn.php'; $id = $_POST['id']; if(empty($id)){ echo 0; }else { $stmt = $conn->prepare("DELETE FROM todos WHERE id=?"); $res = $stmt->execute([$id]); if($res){ echo 1; }else { echo 0; } $conn = null; exit(); } }else { header("Location: index.php?mess=error"); }css/style.css
//css/style.css body { background: #6d5ce8; } * { padding: 0px; margin: 0px; box-sizing: border-box; } .todo-item { width: 95%; margin: 10px auto; padding: 20px 10px; box-shadow: 0 4px 8px 0 #ccc, 0 6px 20px 0 #ccc; border-radius: 5px; } .todo-item h2 { display: inline-block; padding: 5px 0px; font-size: 17px; font-family: sans-serif; color: #555; } .todo-item small { display: block; width: 100%; padding: 5px 0px; color: #888; padding-left: 30px; font-size: 14px; font-family: sans-serif; } .remove-to-do { display: block; float: right;font-size:20px; width: 35px; height: 35px; color: rgb(139, 97, 93); text-decoration: none; padding: 0px 5px 8px 10px; border-radius: 50%; transition: background 1s; cursor: pointer; } .remove-to-do:hover { background: rgb(139, 97, 93); color: #fff; } .checked { color: #999 !important; text-decoration: line-through; } .todo-item input { margin: 0px 5px; } .empty { font-family: sans-serif; font-size: 16px; text-align: center; color: #cccc; }